-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell sorting
50 lines (43 loc) · 944 Bytes
/
shell sorting
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
//
// main.c
// shell sort
//
// Created by 毛遵杰 on 2019/12/11.
// Copyright © 2019 毛遵杰. All rights reserved.
//
#include <stdio.h>
void Print(int a[], int n, int i) {
printf("i\n");
for (int j = 0; j<8; j++) {
printf("a[j]\n");
}
printf("\n");
}
void ShellInsertSort(int a[], int n, int dk)
{
for (int i = dk; i<n; ++i) {
if (a[i] < a[i - dk]) {
int j = i - dk;
int x = a[i];
a[i] = a[i - dk];
while (j >= 0 && x < a[j]) {
a[j + dk] = a[j];
j -= dk;
}
a[j + dk] = x;
Print(a, n, i);
}
}
void ShellSort(int a[], int n) {
int dk = n / 2;
while (dk >= 1) {
ShellInsertSort(a, n, dk);
dk = dk / 2;
}
}
int main(void) {
int a[8] = {12,2,16,30,28,10,16,20,6,18};
ShellSort(a, 10);
Print(a, 10, 10);
return 0;
}