forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellSort.c
More file actions
48 lines (37 loc) · 1014 Bytes
/
shellSort.c
File metadata and controls
48 lines (37 loc) · 1014 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shellSort(int array[], int value){
int i = value;
int j, k, tmp;
for (i = value / 2; i > 0; i = i / 2){
for (j = i; j < value; j++){
for(k = j - i; k >= 0; k = k - i){
if (array[k+i] >= array[k]){
break;
}
else{
tmp = array[k];
array[k] = array[k+i];
array[k+i] = tmp;
}
}
}
}
}
int main(){
int array[20];
int range = 500;
for(int i = 0; i < 100; i++){
array[i] = rand() % range + 1;
}
int size = sizeof array / sizeof array[0];
clock_t start = clock();
shellSort(array,size);
clock_t end = clock();
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("Data Sorted\n");
printf("%s\n", "Shell Sort Big O Notation:\n--> Best Case: O(n log(n))\n--> Average Case: depends on gap sequence\n--> Worst Case: O(n)\n");
printf("Time spent sorting: %f\n", time_spent);
return 0;
}