Skip to main content

希尔排序

function shellSort(arr) {
let temp
let n = arr.length
// `n >> 1` 相当于 `Math.floor(n / 2)`.
for (let gap = n >> 1; gap > 0; gap = gap >> 1) {
for (let i = gap; i < n; i++) {
temp = arr[i]
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
arr[j + gap] = arr[j]
}
arr[j + gap] = temp
}
}
return arr
}