forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSort.c
70 lines (63 loc) · 1.48 KB
/
InsertionSort.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
62
63
64
65
66
67
68
69
70
// Program - Insertion Sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int array[1000000];
float timedifference_msec(struct timeval t0, struct timeval t1)
{
return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
}
int main()
{
struct timeval t0;
struct timeval t1;
float elapsed;
int i,key,ch,position;
long size,limit;
printf("Enter number of Elements: ");
scanf("%ld",&size);
int array[size];
printf("Enter Upper limit in place values: ");
scanf("%ld",&limit);
int num=size;
int j,x,y,temp;
srand( (unsigned) time(NULL) * getpid());
gettimeofday(&t0, NULL);
if(array != NULL)
{
for(j = 0; j < num; j++)
{
array[j] = rand()%limit;
}
}
gettimeofday(&t1, NULL);
elapsed = timedifference_msec(t0, t1);
printf("\n");
printf("List of Numbers:\n");
for(j = 0; j < num; j++)
{
printf("%d\n",array[j]);
}
printf("Random Generation code executed in %f milliseconds.\n", elapsed);
printf("\n");
gettimeofday(&t0, NULL);
for (i=1;i<num;i++)
{
position=array[i];
j=i-1;
while(j>=0 && array[j]>position)
{
array[j+1] = array[j];
j = j - 1;
}
array[j + 1] = position;
}
gettimeofday(&t1, NULL);
elapsed = timedifference_msec(t0, t1);
printf("Insertion Sorted List of Numbers:\n");
for(j = 0; j < num; j++)
{
printf("%d\n",array[j]);
}
printf("Insertion Sort Code executed in %f milliseconds.\n", elapsed);
}