Skip to content

Instantly share code, notes, and snippets.

@monkyz
Forked from guipn/qsort.js
Created September 22, 2012 04:20
Show Gist options
  • Save monkyz/3765103 to your computer and use it in GitHub Desktop.
Save monkyz/3765103 to your computer and use it in GitHub Desktop.
Simple quicksort implementations
// 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