-
-
Save monkyz/3765103 to your computer and use it in GitHub Desktop.
Simple quicksort implementations
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
// The simplest implementation I can write. | |
function qsort(array) { | |
var lower, upper, pivot; | |
if (array.length <= 1) { | |
return array; | |
} | |
pivot = array.pop();//The pop() method removes the last element of an array, and returns that element. | |
lower = array.filter(function (value) { | |
return value <= pivot; | |
}); | |
upper = array.filter(function (value) { | |
return value > pivot; | |
}); | |
return qsort(lower). | |
concat([pivot]). | |
concat(qsort(upper)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment