Created
March 20, 2020 03:17
-
-
Save sugarshin/27da7dd54c161c49c6cd89b66c92d275 to your computer and use it in GitHub Desktop.
Revisions
-
sugarshin renamed this gist
Mar 20, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
sugarshin created this gist
Mar 20, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ 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); }