Created
March 20, 2020 03:17
-
-
Save sugarshin/27da7dd54c161c49c6cd89b66c92d275 to your computer and use it in GitHub Desktop.
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
| function quicksort(arr) { | |
| const pivot = arr[Math.floor((arr.length - 1) / 2)]; | |
| const progress = arr.reduce( | |
| (ret, n) => { | |
| const first = Array.isArray(ret[0]) ? ret[0] : []; | |
| const second = Array.isArray(ret[1]) ? ret[1] : []; | |
| if (n < pivot) { | |
| if (first[0] && first[0] > n) { | |
| first.unshift(n); | |
| } else { | |
| first.push(n); | |
| } | |
| } else { | |
| if (second[0] && second[0] > n) { | |
| second.unshift(n); | |
| } else { | |
| second.push(n); | |
| } | |
| } | |
| return [first, second]; | |
| }, | |
| [] | |
| ); | |
| return progress.map(a => (a.length <= 2 || a.every(n => n === a[0])) ? a : quicksort(a)).flat(Infinity); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment