Skip to content

Instantly share code, notes, and snippets.

@galenweber
Created April 24, 2016 16:19
Show Gist options
  • Save galenweber/7a670b1707cf35bfaa4ab5a8343000e8 to your computer and use it in GitHub Desktop.
Save galenweber/7a670b1707cf35bfaa4ab5a8343000e8 to your computer and use it in GitHub Desktop.
var bubbleSort = function(array) {
var madeSort = false;
for (var i=1; i < array.length; i++) {
if (array[i] < array[i-1]) {
var temp = array[i];
array[i] = array[i-1];
array[i-1] = temp;
madeSort = true;
}
}
console.log(counter, array)
// I believe this is proper tail call optimization (PTC)
// Since the ternary operation is done before the function call
// See http://bit.ly/1NqCpV2 (You Don't Know JS)
return madeSort ? bubbleSort(array) : array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment