Stooge Sort

Recursively sorts first 2/3, last 2/3, then first 2/3 again. Named after The Three Stooges. Extremely slow — purely educational.

Best O(n^2.71) Avg O(n^2.71) Worst O(n^2.71) Space O(log n) Stable No In-place Yes Comparison-based

How it works

Recursively sorts first 2/3, last 2/3, then first 2/3 again. Named after The Three Stooges. Extremely slow — purely educational.

Implementation

function stoogeSort(arr, stats) {
  function stooge(lo, hi) {
    compare(lo, hi);
    if (arr[lo] > arr[hi]) {
      [arr[lo], arr[hi]] = [arr[hi], arr[lo]];
      swap(lo, hi);
    }
    if (hi - lo + 1 > 2) {
      const t = Math.floor((hi - lo + 1) / 3);
      stooge(lo, hi - t);
      stooge(lo + t, hi);
      stooge(lo, hi - t);
    }
  }
  if (arr.length > 1) stooge(0, arr.length - 1);
}