From b72d56464ed94586215acb7e7133d11ca39745e2 Mon Sep 17 00:00:00 2001 From: yigencong Date: Sat, 24 Feb 2024 13:42:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0shell=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sort/shell-sort.mjs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 sort/shell-sort.mjs 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) + } + +} +