提供一个h,调整插入排序的间隔,直到h降级为1,变成插入排序

    1. const { less, exch } = require('./util');
    2. const arr1 = [2,3,1,123,5,24]
    3. function shell(arr) {
    4. let N = arr.length;
    5. let h = 1;
    6. while(h < (N / 3)) h = 3 * h + 1;
    7. while(h !== 0) {
    8. for (let i = h; i < N; i++) {
    9. for (j = i; j >= h && less(arr[j], arr[j - h]); j -= h) {
    10. exch(arr, j, j - h);
    11. }
    12. }
    13. h = Math.floor(h / 3);
    14. }
    15. return arr;
    16. }
    17. console.log(shell(arr1));