Skip to content

Instantly share code, notes, and snippets.

@RedGhoul
Created April 18, 2019 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedGhoul/5076cbff85315a7bdae98a50940e56af to your computer and use it in GitHub Desktop.
Save RedGhoul/5076cbff85315a7bdae98a50940e56af to your computer and use it in GitHub Desktop.
// 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