6.shellSort - Javascript
6.shellSort - Javascript
const arr = [5,1,3,2,1,4]
function shellSort(arr) {
let gap = Math.floor(arr.length / 2);
while (gap > 0) {
for (let i = 0; i < arr.length - gap; i++) {
let currentIndex = i;
let gapShiftedIndex = i + gap;
while (currentIndex >= 0) {
if (arr[gapShiftedIndex] <= arr[currentIndex]) {
const temp = arr[currentIndex];
arr[currentIndex] = arr[gapShiftedIndex];
arr[gapShiftedIndex] = temp;
}
gapShiftedIndex = currentIndex;
currentIndex -= gap;
}
}
gap = Math.floor(gap / 2);
}
return arr;
}
shellSort(arr);
console.log(arr)
Leave a comment