-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab 3_Prog 1.c
61 lines (59 loc) · 1.39 KB
/
Lab 3_Prog 1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//Q1 To compare the best case, worst case and average case time complexity of Insertion sort
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
double d;
int insertionsort(int arr[], int n)
{
clock_t start = clock();
int i, key, j,c1=0,c2=0;
for (i = 1; i < n; i++) {
c1++;
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
c2++;
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
sleep(1);
clock_t end = clock();
d = (double)(end-start)/CLOCKS_PER_SEC;
return c1+c2;
}
void descending(int number[],int n)
{
int i,j,a;
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
}
int main()
{
int n;
printf("Enter the number of elements:- ");
scanf("%d",&n);
int ar[n];
for(int i=0;i<n;i++)
ar[i]=rand();
printf("\tStep Count\t\tClock Time\n");
int a = insertionsort(ar,n);
printf("Average Case:-\t%d\t\t\t%f\n",a,d);
a = insertionsort(ar,n);
printf("Best Case:-\t%d\t\t\t%f\n",a,d);
descending(ar,n);
a = insertionsort(ar,n);
printf("Worst Case:-\t%d\t\t\t%f\n",a,d);
return 0;
}