diff --git a/sort/shell-sort.mjs b/sort/shell-sort.mjs new file mode 100644 index 0000000..b50020e --- /dev/null +++ b/sort/shell-sort.mjs @@ -0,0 +1,33 @@ +import { generateRandomArray, isSort } from "../util/index.mjs"; + +/** + * + * @param {number[]} arr + */ +export function shellSort(arr){ + const n = arr.length + // 创建希尔序列,以克努特序列为例 + let gap = 1; + while (gap < n / 3) { + gap = gap*3 +1 + } + + while(gap>=1) { + // 正常插入排序 + for (let i = 0;i0 && arr[j-gap] > temp) { + arr[j] = arr[j-gap] + j-=gap + } + arr[j] = temp + } + } + // 正常插入排序 + + gap = Math.floor(gap/3) + } + +} +