Created
March 8, 2017 03:55
-
-
Save spasiu/828d031817584c888921df4a41055b33 to your computer and use it in GitHub Desktop.
Quicksort in JavaScript
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 qs = (compare, list) => { | |
| if (list.length < 2) { | |
| return list | |
| } | |
| const pivot = list[0] | |
| const left = [] | |
| const right = [] | |
| for (const item of list.slice(1)) { | |
| compare(pivot, item) ? left.push(item) : right.push(item) | |
| } | |
| return qs(compare, left).concat([pivot]).concat(qs(compare, right)) | |
| } | |
| qs((a, b) => a > b, [0,9,3,4,1,2,5,7,6,8]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment