Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Easier, faster quicksort in javascript
// var arr = [1,3,5,2,80,3,71,12];
function sort(arr) {
// base case
if (arr.length <= 1) return arr;
var swapPos = Math.floor((arr.length - 1) / 2),
swapVal = arr[swapPos], less = [], more = [];
arr = arr.slice(0, swapPos).concat(arr.slice(swapPos + 1));
arr.forEach(function(v, i, array) {
if (array[i] < swapVal) {
less.push(array[i]);
} else {
more.push(array[i]);
}
});
return sort(less).concat([swapVal], sort(more));
}
// console.log(sort(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.