Skip to content

Instantly share code, notes, and snippets.

@unbug unbug/quicksort
Created May 21, 2016

Embed
What would you like to do?
function quicksort(arr){
//base case
if(arr.length<=1){ return arr;}
//now find swap position and value
var swapPos = Math.floor((arr.length-1)/2),
swapVal = arr[swapPos],
less = [], more = [];
arr = arr.slice(0, swapPos).concat(arr.slice(swapPos+1));
for(var i=0;i<arr.length;i++){
if(arr[i]< swapVal){
less.push(arr[i]);
}else{
more.push(arr[i]);
}
}
return (quicksort(less)).concat([swapVal], quicksort(more));
}
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.