Created
August 26, 2016 08:40
-
-
Save alexeykomov/fdda60945070d22e518a04e403751f6d 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
function bubbleSort(aInput) { | |
const output = [...aInput]; | |
for (let padding = 0; padding < output.length; padding++) { | |
for (let counter = 1; counter < output.length - padding; counter++){ | |
const prev = output[counter - 1]; | |
const next = output[counter]; | |
if (prev > next) { | |
swap(output, counter - 1, counter); | |
} | |
} | |
} | |
return output; | |
} | |
function swap(aArray, aPrevIndex, aNextIndex) { | |
const temp = aArray[aPrevIndex]; | |
aArray[aPrevIndex] = aArray[aNextIndex]; | |
aArray[aNextIndex] = temp; | |
} | |
console.log(bubbleSort([8, 1, 3, 4, 5])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment