Sunteți pe pagina 1din 36

EXPERIMENT NO.

-1A

AIM-TO IMPLEMENT ALGORITHM USING ARRAY AS A DATA STRUCTURE


AND ANALYSE ITS TIME COMPLEXITY

SELECTION SORT

ALGORITHM
CODE FOR SELECTION SORT

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int arr[100000],i,search,j,t,small,a;
time_t starttime,endtime;
double tc;

int n=malloc(sizeof(n));
printf("enter the no of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
arr[i]=rand()%100;
}
a=1;
starttime=clock();
for(i=0;i<n-1;i++)

{
if(arr[i]>arr[i+1])

{
a=2;
break;
}
}
if(a==2)
{
for(i=0;i<n-1;i++)
{
small=i;
for(j=i+1;j<n;j++)

{
if(arr[j]<arr[small])

{
small=j;
}

}
t=arr[i];
arr[i]=arr[small];
arr[small]=t;
}
}
endtime=clock();
tc=difftime(endtime,starttime)*10/CLOCKS_PER_SEC;
printf("EFFICIENCY=%1f",tc);
return 0;
}
TIME COMPLEXITY GRAPH(NO OF ELEMENTS VS TIME)

selection sort
140

120

100

80
best
time

60
average
40 worst

20

0
0 20000 40000 60000 80000 100000 120000
-20
no of elements

RUNNING TIME

NO. OF ELEMENTS BEST CASE AVERAGE CASE WORST CASE

10 0.000028 0.00078 0.00009

100 0.00003 0.00089 0.00099

500 0.000004 0.00123 0.0216

1000 0.00022 0.0345 0.0649

5000 0.00086 0.123 0.346

10000 0.001690 0.678 1.264

30000 0.0049 5.678 11.304

50000 0.0081 15.897 31.826

100000 0.0170 61.234 122.622


EXPERIMENT NO.-1B

AIM-TO IMPLEMENT ALGORITHM USING ARRAY AS A DATA STRUCTURE AND


ANALYSE ITS TIME COMPLEXITY

INSERTION SORT

ALGORITHM
CODE FOR INSERTION SORT

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int arr[100000],i,search,j,t,hole,a;
time_t starttime,endtime;

double tc;
int n=malloc(sizeof(n));
printf("enter the no of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
arr[i]=rand%100;
}
a=1;
starttime=clock();
for(i=0;i<n-1;i++)
{
if(arr[i]>arr[i+1])
{
a=2;
break;
}
}
if(a==2)
{
for(i=1;i<n;i++)
{ hole=arr[i];
j=i-1;
while(j>=0&&arr[j]>hole)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=hole;

}
}
endtime=clock();
tc=difftime(endtime,starttime)*10/CLOCKS_PER_SEC;
printf("EFFICIENCY=%1f",tc);

return 0;
}
TIME COMPLEXITY GRAPH(NO OF ELEMENTS VS TIME)

insertion sort
80

70

60

50

40 best
time

30 average
worst
20

10

0
0 20000 40000 60000 80000 100000 120000
-10
no of elements

RUNNING TIME

NO. OF ELEMENTS BEST CASE AVERAGE CASE WORST CASE

10 0.00004 0.00006 0.00007

100 0.00007 0.00045 0.00059

500 0.00014 0.0098 0.0110

1000 0.0002 0.0234 0.0424

5000 0.0012 0.123 0.272

10000 0.00164 0.345 0.8009

30000 0.00531 3.567 6.882

50000 0.00816 9.955 18.363

100000 0.00949 36.543 72.769


EXPERIMENT NO.-1C

AIM-TO IMPLEMENT ALGORITHM USING ARRAY AS A DATA STRUCTURE


AND ANALYSE ITS TIME COMPLEXITY

BUBBLE SORT

ALGORITHM
CODE FOR BUBBLE SORT

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int arr[100000],i,search,j,t,a;
time_t starttime,endtime;
double tc;
int n=malloc(sizeof(n));
printf("enter the no of elements\n");
scanf("%d",&n);

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

arr[i]=rand()%100//best case i+1;


}
a=1;
starttime=clock();
for(i=0;i<n-1;i++)
{
if(arr[i]>arr[i+1])
{
a=2;
break;
}
}
if(a==2)
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{ if(arr[j]>arr[j+1])
{
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
}
endtime=clock();
tc=difftime(endtime,starttime)*10/CLOCKS_PER_SEC;
printf("EFFICIENCY=%1f",tc);
return 0;
}
TIME COMPLEXITY GRAPH (NO OF ELEMENTS VS TIME)

bubble sort
350

300

250

200
best
time

150
average
100 worst

50

0
0 20000 40000 60000 80000 100000 120000
-50
no of elements

RUNNING TIME

NO. OF ELEMENTS BEST CASE AVERAGE CASE WORST CASE

10 0.00005 0.00006 0.00008

100 0.00007 0.00104 0.002

500 0.00014 0.0123 0.037

1000 0.00022 0.0875 0.170

5000 0.00086 0.456 0.837

10000 0.00168 1.342 3.499

30000 0.00487 14.345 28.713

50000 0.00811 39.897 79.358

100000 0.0166 108.356 316.655


EXPERIMENT NO.-1D

AIM-TO IMPLEMENT ALGORITHM USING ARRAY AS A DATA STRUCTURE AND


ANALYSE ITS TIME COMPLEXITY

MERGE SORT

ALGORITHM
CODE FOR MERGE SORT

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main()

int arr[100000],i,search,j,a;

time_t starttime,endtime;

double tc;

int n=malloc(sizeof(n));

printf("enter the no of elements\n");

scanf("%d",&n);

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

arr[i]=i+1;

a=1;

int arr_size=sizeof(arr)/sizeof(arr[0]);

starttime=clock();

for(i=0;i<n-1;i++)

if(arr[i]>arr[i+1])
{

a=2;

break;

if(a==2)

mergesort(arr,0,arr_size-1);

endtime=clock();

tc=difftime(endtime,starttime)*10/CLOCKS_PER_SEC;

printf("EFFICIENCY=%1f",tc);

return 0;

void mergesort(int input[],int l,int r)

{ if (l < r)

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

mergesort(input, l, m);

mergesort(input, m+1, r);

merge(input, l, m, r);

}
void merge(int arr[], int l, int m, int r)

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

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

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1+ j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2)

if (L[i] <= R[j])

arr[k] = L[i];

i++;

else

{
arr[k] = R[j];

j++;

k++;

while (i < n1)

arr[k] = L[i];

i++;

k++;

while (j < n2)

arr[k] = R[j];

j++;

k++;

}
TIME COMPLEXITY GRAPH (NO OF ELEMENTS VS TIME)

merge sort
0.9
0.8
0.7
0.6
0.5
best
time

0.4
average
0.3 worst
0.2
0.1
0
0 20000 40000 60000 80000 100000 120000
-0.1
no of elements

RUNNING TIME

NO. OF ELEMENTS BEST CASE AVERAGE CASE WORST CASE

10 0.00005 0.11 0.22

100 0.00006 0.125 0.23

500 0.00012 0.134 0.369

1000 0.0021 0.22 0.44

5000 0.00085 0.234 0.468

10000 0.00188 0.237 0.477

30000 0.005 0.258 0.509

50000 0.0082 0.325 0.649

100000 0.0166 0.393 0.783


EXPERIMENT NO.-1E

AIM-TO IMPLEMENT ALGORITHM USING ARRAY AS A DATA STRUCTURE


AND ANALYSE ITS TIME COMPLEXITY

QUICK SORT

ALGORITHM
CODE FOR QUICK SORT

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int arr[100000],i,search,a;
time_t starttime,endtime;
double tc;

int n=malloc(sizeof(n));
printf("enter the no of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
arr[i]=rand()%100;
}
a=1;
int arr_size=sizeof(arr)/sizeof(arr[0]);
starttime=clock();

for(i=0;i<n-1;i++)
{
if(arr[i]>arr[i+1])
{
a=2;
break;
}
}
if(a==2)
{
quicksort(arr,0,arr_size-1);
}

endtime=clock();
tc=difftime(endtime,starttime)*100/CLOCKS_PER_SEC;
printf("EFFICIENCY=%1f",tc);

return 0;
}
void quicksort(int arr[],int start,int end)
{ if(start<end)
{
int pivot=getpivot(arr,start,end);
quicksort(arr,start,pivot-1);
quicksort(arr,pivot+1,end);

}
int getpivot(int arr[],int start,int end)
{

int pivot=arr[start];
int pindex=end;
for(int j=end;j>=start+1;j--)
{
if(arr[j]>=pivot)
{
int temp=arr[j];
arr[j]=arr[pindex];
arr[pindex]=temp;
pindex--;
}
}
int temp=arr[pindex];
arr[pindex]=arr[start];
arr[start]=temp;
return pindex;
}
TIME COMPLEXITY GRAPH(NO OF ELEMENTS VS TIME)

quick sort
25

20

15
time

best
10 average
worst
5

0
0 20000 40000 60000 80000 100000 120000
no of elements

RUNNING TIME

NO. OF ELEMENTS BEST CASE AVERAGE CASE WORST CASE

10 0.000002 1.234 2.345

100 0.000007 1.765 3.567

500 0.000014 2.454 4.792

1000 0.000021 2.678 5.937

5000 0.000087 5.466 10.508

10000 0.000169 7.567 14.456

30000 0.000803 8.234 17.425

50000 0.00167 9.28 18.56

100000 0.00567 10.44 20.88


EXPERIMENT NO.-1F

AIM-TO IMPLEMENT ALGORITHM USING ARRAY AS A DATA STRUCTURE


AND ANALYSE ITS TIME COMPLEXITY

HEAP SORT

ALGORITHM
CODE FOR HEAP SORT

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int arr[100000],i,a;
time_t starttime,endtime;
double tc;

int n=malloc(sizeof(n));
int k=n;
printf("enter the no of elements\n");
scanf("%d",&n);

for(i=0;i<n;i++)
{
arr[i]=k--;
}
int arr_size=sizeof(arr)/sizeof(arr[0]);
a=1;
starttime=clock();
for(int i=0;i<n-1;i++)
{
if(arr[i]>arr[i+1])
{
a=2;
break;
}
}
if(a==2)
{
heapSort(arr,arr_size-1);
}
endtime=clock();
tc=difftime(endtime,starttime)*100/CLOCKS_PER_SEC;
printf("EFFICIENCY=%1f",tc);
return 0;
}
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;

if (l < n && arr[l] > arr[largest])


largest = l;

if (r < n && arr[r] > arr[largest])


largest = r;

if (largest != i)
{
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{

for (int i = n / 2 - 1; i >= 0; i--)


heapify(arr, n, i);
for (int i=n-1; i>=0; i--)
{
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
void swap(int a,int b)
{
int t=a;
a=b;
b=t;

}
TIME COMPLEXITY GRAPH (NO OF ELEMENTS VS TIME)

heap sort
1.6

1.4

1.2

1
time

0.8 best
average
0.6
worst
0.4

0.2

0
0 20000 40000 60000 80000 100000 120000
no of elements

RUNNING TIME

NO. OF ELEMENTS BEST CASE AVERAGE CASE WORST CASE

10 0.0004 0.123 0.245

100 0.0006 0.178 0.345

500 0.0012 0.223 0.456

1000 0.0021 0.235 0.567

5000 0.0088 0.345 0.654

10000 0.0168 0.378 0.789

30000 0.0546 0.445 0.889

50000 0.0851 0.545 1.046

100000 0.20 0.768 1.346


EXPERIMENT NO.-2A

AIM-TO IMPLEMENT ALGORITHM USING ARRAY AS A DATA STRUCTURE


AND ANALYSE ITS TIME COMPLEXITY

LINEAR SEARCH

ALGORITHM
CODE FOR LINEAR SEARCH

#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main()
{
int arr[100000],i,search;
time_t start,end;

double tc;
int n=malloc(sizeof(n));
printf("enter the no of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
arr[i]=i+1;
}
printf("enter the element to be searched\n");
scanf("%d",&search);

start=clock();
int j=0;
int a=1;
while(j<n-1)
{

if(arr[j]==search)
{
a=2;
break;
}
j++;
}
if(a==2)
{

printf("element found\n");
}
else
{
printf("not found\n");
}

end=clock();
tc=difftime(end,start)/CLOCKS_PER_SEC;
printf("EFFICIENCY=%1f",tc);
return 0;
}
TIME COMPLEXITY GRAPH (NO OF ELEMENTS VS TIME)

linear search
1.8
1.6
1.4
1.2
1
time

best
0.8
average
0.6 worst
0.4
0.2
0
0 20000 40000 60000 80000 100000 120000
no of elements

RUNNING TIME

NO. OF ELEMENTS BEST CASE AVERAGE CASE WORST CASE

10 0 0.029 0.035

100 0 0.040 0.067

500 0 0.042 0.079

1000 0 0.047 0.094

5000 0 0.072 0.154

10000 0 0.166 0.176

30000 0 0.239 0.548

50000 0 0.377 0.984

100000 0 0.389 1.494


EXPERIMENT NO.-2B

AIM-TO IMPLEMENT ALGORITHM USING ARRAY AS A DATA STRUCTURE AND


ANALYSE ITS TIME COMPLEXITY

BINARY SEARCH

ALGORITHM
CODE FOR BINARY SEARCH

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{ int arr[100000],i,search;
time_t start,end;

double tc;
int n=malloc(sizeof(n));
printf("enter the no of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
arr[i]=i+1;
}
printf("enter the element to be searched\n");
scanf("%d",&search);
start=clock();
int start=0;
int end=n-1;
int mid=start+end/2;
while(start<=end)

{
if(arr[mid]==search)
{
a=2;
break;
}
else if(arr[mid]>search)
{
end=mid-1;

}
else
{
start=mid+1;
}
}

if(a==2)
{
printf("element found\n");
}
else
{printf("not found\n");
}
end=clock();
tc=difftime(end,start)/CLOCKS_PER_SEC;
printf("EFFICIENCY=%1f",tc);
return 0;
}
TIME COMPLEXITY GRAPH (NO OF ELEMENTS VS TIME)

binary search
1.8
1.6
1.4
1.2
1
time

best
0.8
average
0.6 worst
0.4
0.2
0
0 20000 40000 60000 80000 100000 120000
no of elements

RUNNING TIME

NO. OF ELEMENTS BEST CASE AVERAGE CASE WORST CASE

10 0.028 0.036 0.049

100 0.078 0.081 0.089

500 0.112 0.116 0.119

1000 0.139 0.178 0.234

5000 0.245 0.346 0.465

10000 0.345 0.456 0.645

30000 0.432 0.653 0.879

50000 0.498 0.875 1.234

100000 0.780 1.134 1.564

S-ar putea să vă placă și