mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
15 lines
373 B
C
15 lines
373 B
C
|
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;
|
||
|
}
|
||
|
}
|