Skip to content

Instantly share code, notes, and snippets.

@bencevans
Created November 20, 2012 13:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bencevans/4117847 to your computer and use it in GitHub Desktop.
Save bencevans/4117847 to your computer and use it in GitHub Desktop.
Bubble Sort
function bubbleSort (sortingArray) {
var goto = sortingArray.length - 1;
for(var key in sortingArray) {
for(var i = 0; i <= goto - 1; i++) {
if(sortingArray[i] > sortingArray[i + 1]) {
// Swap
var temp = sortingArray[i];
sortingArray[i] = sortingArray[i +1];
sortingArray[i + 1] = temp;
}
}
goto--;
}
return sortingArray;
}
var unsorted = [456, 345, 234, 543, 465, 654];
console.log('Unsorted:', unsorted);
console.log('Bubble Sorted:', bubbleSort(unsorted));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment