提供一个h,调整插入排序的间隔,直到h降级为1,变成插入排序
const { less, exch } = require('./util');
const arr1 = [2,3,1,123,5,24]
function shell(arr) {
let N = arr.length;
let h = 1;
while(h < (N / 3)) h = 3 * h + 1;
while(h !== 0) {
for (let i = h; i < N; i++) {
for (j = i; j >= h && less(arr[j], arr[j - h]); j -= h) {
exch(arr, j, j - h);
}
}
h = Math.floor(h / 3);
}
return arr;
}
console.log(shell(arr1));