Created
July 12, 2017 10:18
-
-
Save ramzesucr/c97be169c19c4d8d15a8e112df6dcd01 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 bubbleSort = (input) => { | |
let data = input.slice() | |
for(var i = 0; i < data.length; i++) { | |
for(var j = 1; j < data.length - i; j++) { | |
if(data[j - 1] > data[j]) exchange(data, j - 1, j) | |
} | |
} | |
return data | |
} | |
const exchange = (list, left, right) => { | |
const mid = list[left] | |
list[left] = list[right] | |
list[right] = mid | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment