Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Created January 26, 2013 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroshi-maybe/4641941 to your computer and use it in GitHub Desktop.
Save hiroshi-maybe/4641941 to your computer and use it in GitHub Desktop.
Bubble sort implemented with Javascript.
var bubbleSort = function(array) {
for (var i=0, length=array.length; i<length; i+=1) {
for (var j=0; j<array.length-i-1; j+=1) {
if (array[j]>array[j+1]) {
var smallNum = array[j+1];
array[j+1]=array[j];
array[j]=smallNum;
}
}
}
return array;
};
console.log(bubbleSort([5,1,3,2,1,0,10]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment