Skip to content

Instantly share code, notes, and snippets.

@gvinter
Created June 23, 2011 14:23
Show Gist options
  • Save gvinter/1042629 to your computer and use it in GitHub Desktop.
Save gvinter/1042629 to your computer and use it in GitHub Desktop.
BubbleSort
function bubble_sort(an_array){
var n = an_array.length;
var found_inversion = true;
while(found_inversion){
found_inversion = false;
for (var i=1;i<n;i++){
if(an_array[i-1] > an_array[i]) {
found_inversion = true;
swap(an_array, i-1, i);
}
}
}
return an_array;
}
console.log(bubble_sort(4,5,2,3,1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment