Documente Academic
Documente Profesional
Documente Cultură
-1A
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
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
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++)
{
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
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));
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;
{ if (l < r)
{int m = l+(r-l)/2;
mergesort(input, l, m);
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;
i = 0;
j = 0;
k = l;
arr[k] = L[i];
i++;
else
{
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
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
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
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 (largest != i)
{
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{
}
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
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
10 0 0.029 0.035
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