Created
July 6, 2016 05:24
-
-
Save minsooshin/e31a1b0979d0e588b228d4b1459db5ef 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
const swap = (array, a, b) => { | |
const temp = array[a]; | |
array[a] = array[b]; | |
array[b] = temp; | |
return array; | |
}; | |
function bubbleSort(array) { | |
const n = array.length; | |
let swapped = false; | |
do { | |
swapped = false; | |
for (let i = 0; i < n; i++) { | |
if (array[i] > array[i + 1]) { | |
swap(array, i, i + 1); | |
swapped = true; | |
} | |
} | |
} while (swapped) | |
return array; | |
} | |
console.log(bubbleSort([76, 23, 4, 7, 11])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment