-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdemo_shell_sort_1.cpp
More file actions
68 lines (58 loc) · 1.2 KB
/
demo_shell_sort_1.cpp
File metadata and controls
68 lines (58 loc) · 1.2 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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;
long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;
// 指定了间隔g的插入排序
void insertionSort(int A[], int n, int g) {
for (int i = g; i < n; i++) {
int v = A[i];
int j = i - g;
while(j >= 0 && A[j] > v) {
A[j + g] = A[j];
j -= g;
cnt++;
}
A[j + g] = v;
}
}
void shellSort(int A[], int n) {
// 生成数列 G={1, 4, 13, 40, 121, 364, 1093,}
for (int h = 1; ;) {
if (h > n) break;
G.push_back(h);
h = 3 * h + 1;
}
for (int i = G.size() - 1; i >= 0; i--) { // 按逆序指定G[i] = g
insertionSort(A, n, G[i]);
}
}
/**
* 5
* 5
* 1
* 4
* 3
* 2
*/
int main() {
cin >> n;
// 使用速度更快的scanf 函数进入输入
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
cnt = 0;
shellSort(A, n);
cout << G.size() <<endl;
for (int i = G.size() - 1; i >= 0; i--) {
printf("%d", G[i]);
if (i) printf(" ");
}
printf("\n");
printf("%d\n", cnt);
for (int i = 0; i < n; i++) printf("%d\n", A[i]);
return 0;
}