-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortingInsertion.c
More file actions
76 lines (63 loc) · 1.8 KB
/
sortingInsertion.c
File metadata and controls
76 lines (63 loc) · 1.8 KB
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
71
72
73
74
75
76
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; i++) {
int element = arr[i];
int j = i-1;
while (j >= 0 && arr[j] > element) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = element;
}
return;
}
void print(int arr[], int size) {
printf("Sorted Array: ");
printf("[%d, ", arr[0]);
for (int i = 1; i < size-1; i++) {
printf("%d, ", arr[i]);
}
printf("%d]\n", arr[size-1]);
return;
}
// int main() {
// clock_t start, end;
// double cpu_time_used;
// int array[size_];
// for (int i = 0; i < size_; i++) {
// array[i] = size_ * (i-10);
// }
// start = clock();
// insertionSort(array, size_);
// end = clock();
// cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
// print(array, size_);
// printf("%d\n", size_);
// printf("Execution time: %f seconds\n", cpu_time_used);
// return 0;
// }
// int main() {
// clock_t start, end;
// double cpu_time_used;
// int sizes[] = {5, 50, 500, 5000, 50000, 500000, 5000000};
// int num_sizes = sizeof(sizes) / sizeof(sizes[0]);
// srand(time(NULL));
// for (int k = 0; k < num_sizes; k++) {
// int size_ = sizes[k];
// int* array = malloc(sizeof(int) * size_);
// for (int i = 0; i < size_; i++) {
// array[i] = rand() % size_ + 1;
// }
// start = clock();
// insertionSort(array, size_);
// end = clock();
// cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
// printf("Size: %d\n", size_);
// printf("Execution time: %f seconds\n", cpu_time_used);
// free(array);
// }
// return 0;
// }