in my shell sort code, i have used a expression to decrement the 1st for loop and it's seems not working that's why my loop runs infinitely, can someone explain why the decrementing expression is not working ?. Here is the Code.....
const shellSort = function (arr) {
let gap;
for (gap = Math.floor(arr.length / 2); gap >= 1; Math.floor(gap / 2)) {
for (let j = gap; j < n; j++) {
for (let i = j - gap; i >= 0; i - gap) {
if (arr[i + gap] > arr[i]) break;
else [arr[i], arr[i + gap]] = [arr[i + gap], arr[i]];
}
}
}
return arr;
};
Both loop steps are incorrect, you can't have just
because what you mean is
Same applies to the
iloop.Also,
nis undefined there.Your code corrected