From aed06b9ad57931d0b9b5857cc6e71cac7f38570a Mon Sep 17 00:00:00 2001 From: yigencong Date: Sat, 24 Feb 2024 14:45:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=B8=8C=E5=B0=94?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E7=9A=84=E5=B8=8C=E5=B0=94=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sort/shell-sort.mjs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/sort/shell-sort.mjs b/sort/shell-sort.mjs index b50020e..12e6a52 100644 --- a/sort/shell-sort.mjs +++ b/sort/shell-sort.mjs @@ -1,4 +1,21 @@ import { generateRandomArray, isSort } from "../util/index.mjs"; +/* +希尔排序的性能与所选的间隔序列(shell序列)有关。以下是几种常见的希尔排序序列: +Hibbard 序列: +H(k) = 2^k - 1 +如:1, 3, 7, 15, 31, ... + +Sedgewick 序列: +H(k) = 9 * 4^k - 9 * 2^k + 1 或 4^k - 3 * 2^k + 1 +如:1, 5, 19, 41, 109, ... + +Knuth 序列: +H(k) = 3^k + 1 +如:1, 4, 13, 40, 121, ... + +这些序列在实际应用中可能产生不同的性能效果,最佳序列可能取决于特定问题或数据集的性质。 + +*/ /** * @@ -31,3 +48,24 @@ export function shellSort(arr){ } +/** + * @description 使用希尔序列排序 + * @param {number[]} arr + */ +export function shellSort2(arr) { + let n = arr.length + let gap = Math.floor(n / 2) + while (gap > 0) { + for(let i = 0;i 0 && arr[j-gap] > temp){ // 插入过程s + arr[j] = arr[j-gap] + j-=gap + } + arr[j] = temp + } + } + gap = Math.floor(gap / 2) + } +}