Please can you tell me what is wrong to this implementation of bubble sort algorithm in JavaScript?
for (var i=1; i<records.length; i++){
for (var j=records.length; j<1; j--){
if (parseInt(records[i-1]) < parseInt(records[i])){
var temp = records[i-1];
records[i-1] = records[i]
records[i] = temp;
}
}
}
the second
for
loop is coded wrong it should beMy solution:
Shouldn't that be
A simple implementation in ES6 JavaScript will be
Couple of codes for bubble sort
bubblesort should not be used for larger arrays, can be used for smaller ones for its simplicity.
Method 1
Method 2
Method 3
I believe that in a bubble sort, once the i loop has completed an iteration, then the i'th element is now in its correct position. That means that you should write the j loop as
Otherwise your bubble sort will be (even more) inefficient.