Created
October 31, 2015 11:10
-
-
Save khanghoang/52d6dd35a369ccb0afac to your computer and use it in GitHub Desktop.
Quick sort
This file contains hidden or 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
function quicksort(array) { | |
var sortedArray = array.slice(0); // clone the array | |
if(sortedArray.length == 0) return []; | |
var left = [], right = [], pivot = sortedArray[0]; | |
for (var i = 1; i < sortedArray.length; i++) { | |
if(sortedArray[i] < pivot) { | |
left.push(sortedArray[i]); | |
} else { | |
right.push(sortedArray[i]); | |
} | |
} | |
return quicksort(left).concat(pivot, quicksort(right)); | |
} | |
module.exports = quicksort; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment