Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Note this is without any optimizations
const bubblesort = (arr) => {
// We have a loop here to slowly shrink how much of the array we cover
// So that we don't constantly keep looping over the whole array
for(let i = arr.length; i > 0; i--){
// This loop, covers the length of the array given to us by the
// loop above.
for(let j = 0; j < i - 1; i ++){
// Here we do the comparison that asks the question
// "does the value I am currently at greater then its neighbor ?"
// if it is, then we switch the values around, if it isn't we do nothing
if(arr[j] > arr[j+1]){
let tempVal = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tempVal;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment