Created
May 21, 2016 03:30
-
-
Save unbug/18d82492778dc980e717c6c8e604f7e3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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