Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Created January 8, 2019 11:50
Show Gist options
  • Save lsongdev/c7976c28f12d52d3719b6fd620072d2b to your computer and use it in GitHub Desktop.
Save lsongdev/c7976c28f12d52d3719b6fd620072d2b to your computer and use it in GitHub Desktop.
const quicksort = arr => {
if(arr.length <= 1) return arr;
const pivotIndex = arr.length / 2;
const pivot = arr.splice(pivotIndex, 1)[0];
const left = [];
const right = [];
for(var i = 0; i < arr.length; i++){
if(arr[i] < pivot){
left.push(arr[i]);
}else{
right.push(arr[i]);
}
}
return quicksort(left).concat([ pivot ], quicksort(right));
};
console.log(quicksort([ 22, 35, 27, 11, 9, 48, 16, 99, 2 ]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment