Skip to content

Instantly share code, notes, and snippets.

@sugarshin
Created March 20, 2020 03:17
Show Gist options
  • Select an option

  • Save sugarshin/27da7dd54c161c49c6cd89b66c92d275 to your computer and use it in GitHub Desktop.

Select an option

Save sugarshin/27da7dd54c161c49c6cd89b66c92d275 to your computer and use it in GitHub Desktop.

Revisions

  1. sugarshin renamed this gist Mar 20, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. sugarshin created this gist Mar 20, 2020.
    25 changes: 25 additions & 0 deletions quicksort
    Original 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);
    }