Last active
January 12, 2019 09:39
-
-
Save alexcambose/fefa286515e4c625d8a9e1640bb82f56 to your computer and use it in GitHub Desktop.
JavaScript bubble sort
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
let array = [3,1,4,5,3,2,6]; | |
for(let i = 0; i < array.length; i++) { | |
for(j = i + 1; j < array.length; j++) { | |
if(array[i] > array[j]) { | |
// swap | |
const temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} | |
} | |
} | |
console.log(array); // [1, 2, 3, 3, 4, 5, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment