feat: 添加优化后的希尔排序,使用希尔序列
This commit is contained in:
parent
26e211c792
commit
6513a1e5be
@ -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; i<n;i++){
|
||||
// 插入过程
|
||||
let temp = arr[i]
|
||||
let j = i
|
||||
while(j>0 && arr[j-gap] > temp) {
|
||||
arr[j] = arr[j-gap]
|
||||
j-=gap
|
||||
}
|
||||
arr[j] = temp
|
||||
}
|
||||
gap = Math.floor(gap/2)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user