Why do we use `length-i-1` in the inner loop of bubble sort algorithm
Using javascript, It's programmed to sort the elements in an array by asc order. I tried my best to understand why the inner loop uses length-i-1, but couldn't. Can anyone please help me understand why do we use it?
function bubbleSort(arr) {
for(let i=0; i<= arr.length; i++) {
for(let j=0; j< arr.length-i-1; j++) {
if(arr[j] > arr[j+1]) {
let lesser = arr[j+1];
arr[j+1] = arr[j];
arr[j] = lesser;
}
}
}
return arr;
}