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, lo = 0, hi = arr.length - 1) {
  if (arr[lo] > arr[hi])
    [arr[lo], arr[hi]] = [arr[hi], arr[lo]];
  if (hi - lo + 1 > 2) {
    let t = Math.floor((hi - lo + 1) / 3);
    stoogeSort(arr, lo, hi - t);
    stoogeSort(arr, lo + t, hi);
    stoogeSort(arr, lo, hi - t);
  }
}