Created
April 18, 2019 15:11
-
-
Save gerrard00/26c00b96b9c4000a10727c1de7660333 to your computer and use it in GitHub Desktop.
Quicksort in JS
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
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