Created
May 21, 2016 03:30
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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