Skip to content

Instantly share code, notes, and snippets.

@unbug
Created May 21, 2016 03:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save unbug/18d82492778dc980e717c6c8e604f7e3 to your computer and use it in GitHub Desktop.
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