Skip to content

Instantly share code, notes, and snippets.

@cdmz
Created January 28, 2016 23:00
Show Gist options
  • Save cdmz/1062468cd8e31773eea9 to your computer and use it in GitHub Desktop.
Save cdmz/1062468cd8e31773eea9 to your computer and use it in GitHub Desktop.
Bubble sort javascript
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