Created
April 24, 2016 16:19
-
-
Save galenweber/7a670b1707cf35bfaa4ab5a8343000e8 to your computer and use it in GitHub Desktop.
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 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