-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSort.c
57 lines (45 loc) · 968 Bytes
/
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
clock_t start, end;
int arr[100000];
double cpu_time;
void printArray(int arr[], int n)
{
for(int i = 0; i<n; i++)
printf("%d\t", arr[i]);
printf("\n");
}
void insertionSort(int arr[], int n)
{
for(int i = 1; i<n; i++)
{
int hole = i;
int val = arr[i];
while(hole>0 && arr[hole-1]>val)
{
arr[hole] = arr[hole-1];
hole -= 1;
}
arr[hole] = val;
}
}
int main()
{
int n,i;
printf("Enter the size of array: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
arr[i] = rand()%9001+1000;
}
printf("The unsorted array is: \n");
printArray(arr,n);
start = clock();
insertionSort(arr,n);
end = clock();
cpu_time = (double)(end-start)/CLOCKS_PER_SEC;
printf("The sorted array is: \n");
printArray(arr,n);
printf("The time taken is: %lf\n", cpu_time);
}