-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshell_sort.c
47 lines (43 loc) · 1.15 KB
/
shell_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
#include <stdio.h>
void shell_sort_split_half(int arr[], int n) {
int i, j, dk, temp;
for (dk = n >> 1; dk > 0; dk = dk >> 1) {
for (i = dk; i < n; i++) {
temp = arr[i];
for (j = i - dk; j >= 0 && arr[j] > temp; j -= dk)
arr[j + dk] = arr[j];
arr[j + dk] = temp;
}
}
}
void shell_insert(int arr[], int n, int dk) {
int i, j, temp;
for (i = dk; i < n; i += dk) {
temp = arr[i];
j = i - dk;
while (j >= 0 && temp < arr[j]) {
arr[j + dk] = arr[j];
j -= dk;
}
arr[j + dk] = temp;
}
}
void shell_sort(int arr[], int n, int dlta[], int t) {
int k;
for (k = 0; k < t; ++k) {
// 一趟增量为 dlta[k] 的插入排序
shell_insert(arr, n, dlta[k]);
}
}
int main() {
int arr[] = {5, 4, 7, 9, 8, 2, 3, 5, 6, 1};
int n = sizeof(arr) / sizeof(*arr);
int dlta[] = {5, 3, 1};
int t = sizeof(dlta) / sizeof(*dlta);
// shell_sort_split_half(arr, n);
shell_sort(arr, n, dlta, t);
printf("Sort result:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}