Skip to content

Instantly share code, notes, and snippets.

@egermano
Created September 11, 2012 15:17
Show Gist options
  • Save egermano/3699589 to your computer and use it in GitHub Desktop.
Save egermano/3699589 to your computer and use it in GitHub Desktop.
Bubble Sort
function bubbleSort(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)
};
var result = bubbleSort([7, 4, 5, 2, 9, 1]);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment