I came across a working code for insertion sort which is as follows:
function insertionSort(array) {
for (let i = 1; i < array.length; i++){
let curr = array[i];
for (var j = i-1; j >= 0 && array[j] > curr; j--){
array[j+1] = array[j];
}
array[j+1] = curr;
}
return array;
}
My question is: shouldn't the j in the line:
array[j+1] = curr;
be out of scope? What am I missing here?