Skip to content

Instantly share code, notes, and snippets.

@JoeCortopassi
Created April 7, 2022 18:50
Show Gist options
  • Save JoeCortopassi/ef5378b1db294283c8dff10ff924f08a to your computer and use it in GitHub Desktop.
Save JoeCortopassi/ef5378b1db294283c8dff10ff924f08a 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