Skip to content

Instantly share code, notes, and snippets.

@gerrard00
Created April 18, 2019 15:11
Show Gist options
  • Save gerrard00/26c00b96b9c4000a10727c1de7660333 to your computer and use it in GitHub Desktop.
Save gerrard00/26c00b96b9c4000a10727c1de7660333 to your computer and use it in GitHub Desktop.
Quicksort in JS
const quicksort = (arr) => {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[0];
const others = arr.slice(1);
return [
...quicksort(others.filter(v => v <= pivot)),
pivot,
...quicksort(others.filter(v => v > pivot)),
];
};
const input = [8, 6, 7, 5, 3, 0, 9];
const result = quicksort(input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment