Created
September 28, 2016 06:04
-
-
Save mitrakmt/5567975351c7ebf025f55f9a532782e4 to your computer and use it in GitHub Desktop.
Exploring bubble sort in JavaScript.
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 sort(values) { | |
var origValues = values.slice(); | |
var length = origValues.length - 1; | |
do { | |
var swapped = false; | |
for(var i = 0; i < length; ++i) { | |
if (origValues[i] > origValues[i+1]) { | |
var temp = origValues[i]; | |
origValues[i] = origValues[i+1]; | |
origValues[i+1] = temp; | |
swapped = true; | |
} | |
} | |
} | |
while(swapped === true); | |
return origValues | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment