mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
16 lines
501 B
C++
16 lines
501 B
C++
// 希尔排序:每一轮按照事先决定的间隔进行插入排序,间隔会依次缩小,最后一次一定要是1。
|
||
template<typename T>
|
||
void shell_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;
|
||
}
|
||
} |