You are on page 1of 4

Program 2

#include<stdio.h>

#include<time.h>

int temp,i,j,a[5000];

clock_t end,start;

double casee;

void bestcase()

for(i=0;i<=5000;i++)

a[i]=i;

start=clock();

for(i=1;i<5000;i++)

for(j=5000;j>=i+1;j--)

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

{ temp=a[j];

a[j]=a[j-1];

a[j-1]=temp;

end=clock();

casee=(double)(end-start)/CLOCKS_PER_SEC;

printf("\n time complexity for best case is :%f \n ",casee);


}

void worstcase()

for(i=0;i<5000;i++)

a[i]=5000-i;

start=clock();

for(i=1;i<5000;i++)

for(j=5000;j>=i+1;j--)

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

{ temp=a[j];

a[j]=a[j-1];

a[j-1]=temp;

end=clock();

casee=(double)(end-start)/CLOCKS_PER_SEC;

printf("\n time complexity for worst case is :%f \n ",casee);

void averagecase()

for(i=0;i<5000;i++)

{
a[i]=rand()%5000;

start=clock();

for(i=1;i<5000;i++)

for(j=5000;j>=i+1;j--)

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

{ temp=a[j];

a[j]=a[j-1];

a[j-1]=temp;

end=clock();

casee=(double)(end-start)/CLOCKS_PER_SEC;

printf("\n time complexity for average case is :%f \n ",casee);

void main()

int s;

printf("1 for best case\n 2 for average case\n 3 for worst case\n");

do

printf("enter choice : ");

scanf("%d",&s);

switch(s)
{

case 1:

bestcase();

break;

case 2:

averagecase();

break;

case 3:

worstcase();

break;

case 4:

break;

default:

printf("enter correct choice pls");

break;

}while(s!=4);

//return 0;

You might also like