-
-
Save zendzo/6c2dfe50de0a98563bf0c3aede34e649 to your computer and use it in GitHub Desktop.
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
const numbers = [13, 5, -3, 7, 6, 4, 1, 17, 0, -1, -2]; | |
function sort(arr) { | |
const sorted = arr.slice(); | |
let swapped; | |
do { | |
swapped = false; | |
for (let i = 0, l = sorted.length; i < l; i++) { | |
if (sorted[i] > sorted[i + 1]) { | |
const temp = sorted[i]; | |
sorted[i] = sorted[i + 1]; | |
sorted[i + 1] = temp; | |
swapped = true; | |
} | |
} | |
} while (swapped); | |
return sorted; | |
} | |
console.log(sort(numbers)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment