0% found this document useful (0 votes)
23 views4 pages

Best, Worst, Average Case of Insertion Sort

The document describes insertion sort and reverse insertion sort algorithms. It generates random arrays of integers, sorts them using the algorithms, and times the sorting operations to compare best, average, and worst case performance.

Uploaded by

nobitanobi0411
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Best, Worst, Average Case of Insertion Sort

The document describes insertion sort and reverse insertion sort algorithms. It generates random arrays of integers, sorts them using the algorithms, and times the sorting operations to compare best, average, and worst case performance.

Uploaded by

nobitanobi0411
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream>

#include<time.h>

const int N=100;

using namespace std;

void insertionsort(long int arr[],long int n)

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

for(int j=i;j>0;j--)

if (arr[j]<arr[j-1])

swap(arr[j],arr[j-1]);

else{

break;

}
}

void reverseinsertionsort(long int arr[],long int n)

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

for(int j=i;j>0;j--)

if (arr[j]>arr[j-1])

swap(arr[j],arr[j-1]);

else{

break;

int main()

long int n=10000;

int it=0;

double tim1[10], tim2[10], tim3[10];

cout << "A_size, Best, Average, Worst\n";


while(it++ <10)

long int best[n],worst[n],avg[n];

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

long int no=rand() % n +1;

best[i]=no;

avg[i]=no;

worst[i]=no;

clock_t start,end;

insertionsort(best,n);

start=clock();

insertionsort(best,n);

end=clock();

tim1[it] = ((double)(end - start));

start=clock();

insertionsort(avg,n);

end=clock();

tim2[it]=((double)(end - start));

reverseinsertionsort(worst,n);

start=clock();

insertionsort(worst,n);
end=clock();

tim3[it]=((double)(end - start));

cout << n << ", " << (long int)tim1[it] << ", " << (long int)tim2[it] << ", " << (long int)tim3[it] << "\n";

n += 10000;

return 0;

You might also like