Skip to content

Instantly share code, notes, and snippets.

@JoeCortopassi
Created December 1, 2021 01:17
Show Gist options
  • Save JoeCortopassi/6cc3520d2bd0ca558107f5082940a411 to your computer and use it in GitHub Desktop.
Save JoeCortopassi/6cc3520d2bd0ca558107f5082940a411 to your computer and use it in GitHub Desktop.
function quickSort(arr) {
if (arr.length <= 1) return arr;
const pivot = arr[Math.floor(arr.length/2)];
const lower = arr.filter(val => val < pivot);
const equal = arr.filter(val => val === pivot);
const higher = arr.filter(val => val > pivot);
return [...quickSort(lower), ...quickSort(equal), ...quickSort(higher)];
}
const test = [6,2,5,3,4,1,9,7,0,8];
console.log(quickSort(test));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment