升序排列数组
function shellSort(arr) { let len = arr.length; for (let h = Math.floor(len / 2); h > 0; h = Math.floor(h / 2)) { for (let i = h; i < len; i++) { for (let j = i - h; j >= 0 && arr[j] > arr[j + h]; j -= h) { [arr[j], arr[j + h]] = [arr[j + h], arr[j]]; } } } return arr;}
算法测试
console.log(shellSort([5, 4, 6, 3, 35, 17, 1]));// [ 1, 3, 4, 5, 6, 17, 35 ]console.log(shellSort([5, 4, 6, 3, 35, 17, 1, 26]));// [ 1, 3, 4, 5, 6, 17, 26, 35 ]console.log(shellSort([22, 1, 5, 9, 12, - 6, 1]));// [ -6, 1, 1, 5, 9, 12, 22 ]