Skip to content

Instantly share code, notes, and snippets.

@ccnixon
Created October 10, 2015 15:43
Show Gist options
  • Save ccnixon/b815d8d52099eae2f392 to your computer and use it in GitHub Desktop.
Save ccnixon/b815d8d52099eae2f392 to your computer and use it in GitHub Desktop.
Bubble Sort
var bubbleSort = function(array) {
var len = array.length;
var swap = function(i,j,array){
var holder = array[i];
array[i] = array[j];
array[j] = holder;
}
for(var j = 0; j < len - 1; j++){
var swaps = false;
for(var i = 0; i < len - 1; i++){
if(array[i] > array[i+1]){
swaps = true;
swap(i, i+1, array)
}
if(!swaps) {break;}
}
}
return array;
}
var test = [33, 4, 2222, 5, 1, -10000, 4]
console.log(bubbleSort(test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment