Skip to content

Instantly share code, notes, and snippets.

@liamgriffiths
Last active January 1, 2016 22:59
Show Gist options
  • Save liamgriffiths/8213546 to your computer and use it in GitHub Desktop.
Save liamgriffiths/8213546 to your computer and use it in GitHub Desktop.
// quick sort
// https://en.wikipedia.org/wiki/Quicksort
var sort = function(list) {
if (list.length <= 1) return list;
var pivot = list.splice(Math.floor(list.length / 2), 1);
var less = [], greater = [];
while (list.length) {
var item = list.shift();
if (item <= pivot) {
less.push(item);
} else {
greater.push(item);
}
}
return sort(less).concat(pivot).concat(sort(greater));
};
module.exports = sort;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment