You are on page 1of 17

PROGRAM-1(a)

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 -

1. First, we have to traverse the array elements using a for loop.


2. In each iteration of for loop, compare the search element with the current array
element, and -
o If the element matches, then print the position of the corresponding array
element.
o If the element does not match, then move to the next element.
3. 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>

void linarSearch(int arr[], int n, int num)


{
auto start = chrono::high_resolution_clock::now();
int i,c=0,pos;
for (i = 0; i < n; i++)
{
if (arr[i] == num)
{
c=1;
pos=i+1;
break;
}
}

if(c==0)
cout << "Number not found !!"<< endl;
else
cout << "Number found at position:"<< " " << pos<< endl;

auto finish = chrono::high_resolution_clock::now();

cout << "\nTime taken:" << chrono::duration_cast<chrono::nanoseconds>(finish -


start).count() << " ns" << 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 << "Enter the number to be searched: ";


cin >> num;

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.

2. The item is compared with the middle element of the list.

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>

void BinarySearch(int arr[], int n, int num)


{
auto start = chrono::high_resolution_clock::now();

int first = 0, last = n - 1, middle = (first + last) / 2;

while (first <= last)


{
if (arr[middle < num])
{
first = middle + 1;
}
else if (arr[middle] == num)
{
cout << num << " "
<< "Found at location -"<< middle + 1 << endl;
break;
}
else
{
last = middle - 1;
}
middle = (first + last) / 2;

if (first > last)


{
cout << "Not found!!"<< " " << num << " is not present in the list:" << endl;
}
}

auto finish = chrono::high_resolution_clock::now();

cout << "\nTime taken:" << chrono::duration_cast<chrono::nanoseconds>(finish -


start).count() << " ns" << endl;
}

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;
}

cout << "Enter the number to be searched:";


cin >> num;

BinarySearch(a, n, num);

return 0;
}

Output:
TIME COMPLEXITY:

Best Case: O(1)


Average Case: O(logn)
Worst Case: O(logn)
PROGRAM-2(a)

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;

void InsertionSort(int arr[], int n) {


    auto start = chrono::high_resolution_clock::now();
  
    int i, key, j;
    for (i = 1; i < n; i++)
  {
        key = arr[i];
        j = i - 1;
    
        while (j >= 0 && arr[j] > key)
    {
            arr[j + 1] = arr[j];
            j = j - 1;
    }
        arr[j + 1] = key;
  }
  
    auto finish = chrono::high_resolution_clock::now();
    cout << "Time taken: " << chrono::duration_cast<chrono::nanoseconds> (finish -
start).count() << "ns" << endl;
}

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;

void BubbleSort(int arr[], int n) {


    auto start = chrono::high_resolution_clock::now();
  
    int i, j;
    for (i = 0; i < n - 1; i++){
        for (j = 0; j < n - i - 1; j++){
            if (arr[j] > arr[j + 1]){
                swap(arr[j], arr[j + 1]);
      }
    }
  }
  
    auto finish = chrono::high_resolution_clock::now();
    cout << "Time taken: " << chrono::duration_cast<chrono::nanoseconds> (finish -
start).count() << "ns" << endl;
}

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:

Best Case : O(n)


Average Case : O(n^2)
Worst Case : O(n^2)
PROGRAM-2(c)

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;

void swap(int &a, int &b) {


   int temp;
   temp = a;
   a = b;
   b = temp;
}

void SelectionSort(int arr[], int n) {


    auto start = chrono::high_resolution_clock::now();
  
    int i, j, imin;
    for(i = 0; i< n-1; i++) {
        imin = i;
        for(j = i+1; j < n ; j++){
            if(arr[j] < arr[imin]){
                imin = j;
      }
    }  
        swap(arr[i], arr[imin]);
  }
  
    auto finish = chrono::high_resolution_clock::now();
    cout << "Time taken: " << chrono::duration_cast<chrono::nanoseconds> (finish -
start).count() << "ns" << endl;
}

void swapping(int &a, int &b) {


   int temp;
   temp = a;
   a = b;
   b = temp;
}

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:

Best Case : O(n^2)


Average Case : O(n^2)
Worst Case : O(n^2)
PROGRAM-2(d)

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;

void merge(int arr[], int p, int q, int r) {


    int n1 = q - p + 1;
    int n2 = r - q;

    int L[n1], M[n2];

    for (int i = 0; i < n1; i++)


        L[i] = arr[p + i];
    for (int j = 0; j < n2; j++)
        M[j] = arr[q + 1 + j];

    int i, j, k;
    i = 0;
    j = 0;
    k = p;

    while (i < n1 && j < n2) {


        if (L[i] <= M[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = M[j];
            j++;
    }
        k++;
  }

    while (i < n1) {


        arr[k] = L[i];
        i++;
        k++;
  }

    while (j < n2) {


        arr[k] = M[j];
        j++;
        k++;
  }
}

void MergeSort(int arr[], int l, int r) {


    if (l < r) {
        int m = l + (r - l) / 2;

        MergeSort(arr, l, m);
        MergeSort(arr, m + 1, r);

        merge(arr, l, m, r);
  }
}

void Driver(int arr[], int l, int r){


    auto start = chrono::high_resolution_clock::now();
    MergeSort(arr, 0, r);
    auto finish = chrono::high_resolution_clock::now();
    cout << "Time taken: " << chrono::duration_cast<chrono::nanoseconds> (finish -
start).count() << "ns" << endl;
}

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)

You might also like