forked from huihut/interview
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.h
More file actions
Latest commit
16 lines (16 loc) · 501 Bytes
/
Copy pathShellSort.h
File metadata and controls
16 lines (16 loc) · 501 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 希尔排序:每一轮按照事先决定的间隔进行插入排序,间隔会依次缩小,最后一次一定要是1。
template<typename T>
voidshell_sort(T array[], int length) {
int h = 1;
while (h < length / 3) {
h = 3 * h + 1;
}
while (h >= 1) {
for (int i = h; i < length; i++) {
for (int j = i; j >= h && array[j] < array[j - h]; j -= h) {
std::swap(array[j], array[j - h]);
}
}
h = h / 3;
}
}