Created
October 10, 2015 15:43
-
-
Save ccnixon/b815d8d52099eae2f392 to your computer and use it in GitHub Desktop.
Bubble Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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