Created
April 18, 2019 19:59
-
-
Save RedGhoul/5076cbff85315a7bdae98a50940e56af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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