diff --git a/sort/shell-sort.mjs b/sort/shell-sort.mjs index 12e6a52..21b12ae 100644 --- a/sort/shell-sort.mjs +++ b/sort/shell-sort.mjs @@ -69,3 +69,25 @@ export function shellSort2(arr) { gap = Math.floor(gap / 2) } } + +/** + * 优化版本 + * @param {number[]} arr + */ +export function shellSort3(arr){ + const n = arr.length + let gap = Math.floor(n/2) + while (gap>0){ // 使用不同的gap对子数组排序 + for(let i = gap; i0 && arr[j-gap] > temp) { + arr[j] = arr[j-gap] + j-=gap + } + arr[j] = temp + } + gap = Math.floor(gap/2) + } +}