-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.c
65 lines (57 loc) · 1.89 KB
/
sorting.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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "mergeSort.h"
#include "insertionSort.h"
int main()
{
int size, *array, option;
unsigned long int comparisons = 0;
unsigned long int *ptrComparisons = &comparisons;
clock_t start, end;
double cpu_used;
printf("1. Merge sort\n");
printf("2. Insertion sort\n");
printf("Choose sorting:\n");
scanf("%d", &option);
// Change this file to read from different input array
freopen("./input/array_seed_thousand_descending.txt", "r", stdin);
// Change this file to write to different file
freopen("./output/log_test.txt", "w", stdout);
printf("Enter array size: \n");
scanf("%d", &size);
array = malloc(sizeof(int) * size);
printf("Enter %d elements: \n", size);
for (int i = 0; i < size; ++i) {
scanf("%d", &array[i]);
}
switch (option){
case 1:
printf("Merge Sort\n");
start = clock();
mergeSort(array, 0, size - 1, ptrComparisons);
end = clock();
cpu_used = ((double) (end - start)) / (double) CLOCKS_PER_SEC;
printf("Total comparisons done: %lu\n", comparisons);
printf("Total CPU time: %.6lf (seconds)\n", cpu_used);
break;
case 2:
printf("Insertion sort\n");
start = clock();
insertionSort(array, size, ptrComparisons);
end = clock();
cpu_used = ((double) (end - start)) / (double) CLOCKS_PER_SEC;
printf("Total comparisons done: %lu\n", comparisons);
printf("Total CPU time: %.6lf (seconds)\n", cpu_used);
break;
default:
printf("Not supported\n");
}
// Uncomment to print the sorted array
// printf("Sorted array: \n");
// for (int j = 0; j < size; ++j) {
// printf("%d ", array[j]);
// }
printf("\n");
return 0;
}