-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion_Sort.c
61 lines (56 loc) · 1.22 KB
/
Insertion_Sort.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int n, i;
/*printf("Enter no of elements you want in your array: ");
scanf("%d\n",&n);
int arr[n];*/
clock_t begin, end;
double total_t;
begin = clock();
FILE * fp;
fp = fopen("1-25000.txt","r");
fscanf(fp, "%d", &n);
int *arr = malloc(n * sizeof(int));
for (i=0;i<n;i++)
{
fscanf(fp, "%d", &arr[i]);
//scanf("%d",&arr[i]);
}
insertionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
end= clock();
total_t = (double)(end - begin );
//printf("Total Numbers : %d\n",n);
printf("Initial time taken by CPU: %d \n", begin );
printf("End time taken by CPU: %d \n", end );
printf("Total time taken by CPU: %f seconds\n", total_t );
free(arr);
return 0;
}