DAA File1
DAA File1
DAA File1
UE218039
EXPERIMENT-1
INTRODUCTION
Linear search- It is a type of algorithm for sequential searching of the
data. This algorithm finds a given element with O(n) complexity. It is
applied to a collection of items. Each and every item of the data is
searched sequentially, and returned if it matches the searched
element. If no matches are found, then the search keeps on
continuing till the end of the collected data. It is basically a
technique of exploring every element while traversing the list. The
searching algorithm can be applied to both the sorted and the
unsorted data. Practically linear search is rarely used because of the
faster searching options provided by other search algorithms like
binary search algorithms and hashtables.
Features of linear search algorithms
LIBRARY USED
#include<iostream>
#include<chrono>
using namespace std;
int main()
{
auto start_time=chrono::system_clock::now();
int el,flag=0,i,s,n;
cout<<"Enter the size of array\n";
cin>>n;
int *arr=new int[n];
cout<<"enter the values in array "<<endl;
for(i=0;i<n;i++)
arr[i]=i;
cout<<"enter the number to be search in array "<<endl;
cin>>el;
for(i=0;i<10;i++)
{
if(arr[i]==el)
{
flag=1;
break;
}
else
continue;
}
auto end_time=chrono::system_clock::now();
if(flag==1)
{
cout<<"number is found "<<endl;
cout<<"and its value is "<<arr[i]<<endl;
cout<<"number found is the "<<s+1<<" element of the array "<<endl;
}
else
cout<<"number chosen is not from array ";
chrono::duration<double> elapsed_time= end_time-start_time;
cout<<"Elapsed time: "<<elapsed_time.count() <<"ms"<<"\n";
return 0;
}
Output-
Graph-
BINARY SEARCH CODE-
#include <iostream>
#include <chrono>
using namespace std;
if (arr[mid] == r)
{
// cout << arr[mid] << endl;
return 1;
break;
}
else if (arr[mid] < r)
{
beg = mid + 1;
}
else
{
end = mid - 1;
}
}
return -1;
};
int main()
{
int r;
int mid, end, beg;
beg = 0;
int arr[500000];
end = sizeof(arr)/sizeof(arr[0]);
for (int i = 0; i < 500000; i++)
{
arr[i] = i;
}
cout<<"enter the value you want to find: ";
cin >> r;
Output-
Graph-
EXPERIMENT-2
Bubble sort - Bubble Sort is the simplest sorting algorithm that works
by repeatedly swapping the adjacent elements if they are in the
wrong order. This algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite high.
Although it is simple to use, it is primarily used as an educational tool
because the performance of bubble sort is poor in the real world. It is
not suitablefor large data sets. The average and worst-case
complexity of Bubble sort is O(n2),where n is a number of items.
First Pass:
The largest element is placed in its correct position, i.e., the end of
the array.
Second Pass:
Place the second largest element at correct position
Third Pass:
Place the remaining two elements at their correct positions.
include<chrono>
int main()
{
auto begin= chrono:: high_resolution_clock::now();
int n;
for(int i=0;i<n;i++)
{
arr[n-1]=i;
}
int t;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(arr[j]<arr[j-1])
{
t=arr[j];
arr[j]=arr[j-1];
arr[j-1]=t;
}
}
}
auto end=chrono::high_resolution_clock::now();
auto elapsed= chrono::duration_cast<chrono::nanoseconds>(end-begin).count();
"; cout<<elapsed;
return 0;
}
Output-
Enter the size of the array 10
1402400000 PS C:\c++>
Graph-
EXPERIMENT-3
Partition is done recursively on each side of the pivot after the pivot
is placed in its correct position and this finally sorts the array.
Choice of Pivot:
There are many different choices for picking pivots.
1.Always pick the first element as a pivot.
2.Always pick the last element as a pivot (implemented below)
3.Pick a random element as a pivot.
4.Pick the middle as the pivot.
Partition Algorithm:
The logic is simple, we start from the leftmost element and keep
track of the index of smaller (or equal) elements as i. While
traversing, if we find a smaller element, we swap the current element
with arr[i]. Otherwise, we ignore the current element.
Time complexity:
○ Worst Case Complexity - In quick soít, woíst case occuís when thepivot
element is eitheí gíeatest oí smallest element. Suppose, if the pivot element
is always the last element of the aííay, the woíst case would occuí when the
given aííay is soíted alíeady in ascending oí descending oídeí. ľhe woíst-
case time complexity of quicksoít is O(n2).
QUICK SORT CODE-
import time
import random
n = random.randint(20000001,20000001)
def in_array():
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
arr1 = in_array()
start_time = time.time()
#print("\nThe sorted array is: ",quicksort(arr1))
quicksort(arr1)
end_time = time.time()