Professional Documents
Culture Documents
Aim: To implement linear search using array as a data structure and analyze its time
complexity
Algorithm:
The steps used in the implementation of Linear Search are listed as follows -
Source Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
#include <time.h>
#include <chrono>
if(c==0)
cout << "Number not found !!"<< endl;
else
cout << "Number found at position:"<< " " << pos<< endl;
int main()
{
int num, n, i, m;
cout << "Enter the array size: ";
cin >> n;
int a[n], b[n], c[n];
// BEST CASE
for (i = 0; i < n; i++)
{
a[i] = i;
}
// AVERAGE CASE
for(int i=0;i<n;i++)
{
m=rand()%n;
if(m!=0)
b[i]=m;
}
// WORST CASE
for(int i=0;i<n;i++)
{
c[i]=n-1;
}
cout<<"Best Case"<<endl;
linarSearch(a,n,num);
cout<<"Average Case"<<endl;
linarSearch(b,n,num);
cout<<"Worst Case"<<endl;
linarSearch(c,n,num);
return 0;
}
Output:
Time complexity:
Best Case: O(1)
Average Case: O(n)
Worst Case: O(n)
PROGRAM-1(b)
Aim: To implement Binary search using array as a data structure and analyze its time
complexity
Algorithm:
The steps used in the implementation of Binary Search are listed as follows –
1. Binary search follows the divide and conquer approach in which the list is divided
into two halves.
3. If the match is found then, the location of the middle element is printed.
4. Otherwise, we search into either of the halves depending upon the result produced
through the match.
5. If there is no match or the search element is not present in the given array, then print
number not found.
Source Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
#include <time.h>
#include <chrono>
int main()
{
int num, n, i, m;
cout << "Enter the array size:";
cin >> n;
int a[n], b[n], c[n];
for (i=0;i<n;i++)
{
a[i]=i;
}
BinarySearch(a, n, num);
return 0;
}
Output:
TIME COMPLEXITY:
Aim: To implement insertion sort using array as a data structure and analyze its time
complexity.
Algorithm:
Following are the steps of implementation:
1. If it is the first element, it is already sorted, return 1.
2. Pick next element.
3. Compare with all elements in the sorted sub-list.
4. Shift all the elements in the sorted sub-list that is greater than the value to be sorted.
5. Insert the value.
6. Repeat until list is sorted.
Source Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <chrono>
using namespace std;
int main() {
int n,i,m;
cout << "Enter the size of array: ";
cin >> n;
int a[n], b[n], c[n];
for (i = 0; i < n; i++){
a[i] = i;
}
for (i = 0; i < n; i++){
m = rand() % n;
if (m != 0){
b[i] = m;
}
}
for (i = 0; i < n; i++){
c[i] = n - i - 1;
}
cout << "Worst Case: \n";
InsertionSort(c,n);
cout << "Average Case: \n";
InsertionSort(b,n);
cout << "Best Case: \n";
InsertionSort(a,n);
}
Output:
Time complexity:
Best Case: O(n)
Average case: O(n^2)
Worst Case: O(n^2)
PROGRAM-2(b)
Aim: To implement insertion sort using array as a data structure and analyze its time
complexity.
Algorithm:
1. Traverse the given array.
2. Compare the element and the next element.
3. If the current element is greater than the next element, then swap both the elements.
Source Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <chrono>
using namespace std;
int main() {
int n,i,m;
cout << "Enter the size of array: ";
cin >> n;
int a[n], b[n], c[n];
for (i = 0; i < n; i++){
a[i] = i;
}
for (i = 0; i < n; i++){
m = rand() % n;
if (m != 0){
b[i] = m;
}
}
for (i = 0; i < n; i++){
c[i] = n - i - 1;
}
cout << "Worst Case: \n";
BubbleSort(c,n);
cout << "Average Case: \n";
BubbleSort(b,n);
cout << "Best Case: \n";
BubbleSort(a,n);
}
Output:
Time Complexity:
Aim: To implement selection sort using array as a data structure and analyze its time
complexity.
Algorithm:
1. Set MIN to location 0.
2. Search the minimum element in the list.
3. Swap the value at location MIN.
4. Increment MIN to point to next element.
5. Repeat until list is sorted.
Source Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <chrono>
using namespace std;
int main() {
int n,i,m;
cout << "Enter the size of array: ";
cin >> n;
int a[n], b[n], c[n];
for (i = 0; i < n; i++){
a[i] = i;
}
for (i = 0; i < n; i++){
m = rand() % n;
if (m != 0){
b[i] = m;
}
}
for (i = 0; i < n; i++){
c[i] = n - i - 1;
}
cout << "Worst Case: \n";
SelectionSort(c,n);
cout << "Average Case: \n";
SelectionSort(b,n);
cout << "Best Case: \n";
SelectionSort(a,n);
}
Output:
Time Complexity:
Aim: To implement merge sort using array as a data structure and analyze its time
complexity.
Algorithm:
MergeSort(arr[], 1, r)
If r>1
1. Find the middle point to divide the array into two halves:
Middle m = (1+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, 1, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, 1, m, r)
Source code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <chrono>
using namespace std;
int i, j, k;
i = 0;
j = 0;
k = p;
MergeSort(arr, l, m);
MergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main() {
int n,i,m;
cout << "Enter the size of array: ";
cin >> n;
int a[n], b[n], c[n];
for (i = 0; i < n; i++){
a[i] = i;
}
for (i = 0; i < n; i++){
m = rand() % n;
if (m != 0){
b[i] = m;
}
}
for (i = 0; i < n; i++){
c[i] = n - i - 1;
}
cout << "Worst Case: \n";
Driver(c,0,n);
cout << "Average Case: \n";
Driver(b,0,n);
cout << "Best Case: \n";
Driver(a,0,n);
}
Output:
Time complexity:
Best Case : O(nlogn)
Average Case : O(nlogn)
Worst Case : O(nlogn)