-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
46 lines (41 loc) · 975 Bytes
/
Copy pathmain.cpp
File metadata and controls
46 lines (41 loc) · 975 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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;
void insertionSort(int A[], int n, int g) {
for (int i = 0; i < n; i++) {
int v = A[i]; // 基準値
int j = i - g; // 基準のindex - ギャップのindex
while(j >= 0 && A[j] > v) {
A[j+g] = A[j];
j -= g;
cnt++;
}
A[j+g] = v;
}
}
void shellSort(int A[], int n) {
// gapをvecotrに詰めていく
for (int h = 1; ; ) {
if (h > n) break;
G.push_back(h);
h = 3*h + 1;
}
// gapの大きい方から順番にinsertionSortに渡していく
for (int i = G.size()-1; i >= 0; i--) {
insertionSort(A, n, G[i]);
}
}
int main() {
cin >> n;
for(int i = 0; i < n; i++) cin >> A[i];
shellSort(A, n);
for (int i = 0; i < n; i++) printf("%d", A[i]);
}