Created
January 28, 2016 23:00
-
-
Save cdmz/1062468cd8e31773eea9 to your computer and use it in GitHub Desktop.
Bubble sort 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 bubble_sort(values) { | |
var length = values.length - 1; | |
do { | |
var swapped = false; | |
for(var i = 0; i < length; ++i) { | |
if (values[i] > values[i+1]) { | |
var temp = values[i]; | |
values[i] = values[i+1]; | |
values[i+1] = temp; | |
swapped = true; | |
} | |
} | |
} | |
while(swapped == true) | |
return values; | |
}; | |
console.log(bubble_sort([7, 4, 5, 2, 9, 1])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment