From 6513a1e5be85df0d90d05b14aa59a16a47b19116 Mon Sep 17 00:00:00 2001 From: yigencong Date: Sat, 24 Feb 2024 15:19:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=90=8E=E7=9A=84=E5=B8=8C=E5=B0=94=E6=8E=92=E5=BA=8F=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=B8=8C=E5=B0=94=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sort/shell-sort.mjs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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) + } +}