-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_sort.c
More file actions
47 lines (38 loc) · 1 KB
/
shell_sort.c
File metadata and controls
47 lines (38 loc) · 1 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
#include <stdio.h>
#include <time.h>
unsigned long long comparacoes = 0;
unsigned long long movimentos = 0;
void shell_sort(int *v, int n) {
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = v[i];
int j;
for (j = i; j >= gap && v[j - gap] > temp; j -= gap) {
v[j] = v[j - gap];
comparacoes++;
movimentos++;
}
v[j] = temp;
movimentos++;
}
}
}
void print_array(int *v, int n) {
for (int i = 0; i < n; i++) {
printf("%d ", v[i]);
}
printf("\n");
}
int main() {
int tam;
scanf("%d", &tam);
int v[tam];
for(int i = 0; i<tam; i++)
scanf("%d", &v[i]);
clock_t start = clock();
shell_sort(v, tam);
clock_t end = clock();
printf("\nComparacoes: %llu\nMovimentos: %llu\n", comparacoes, movimentos);
printf("Tempo: %.6fs\n", (double)(end - start) / CLOCKS_PER_SEC);
return 0;
}