Algorithm Visualiser by Tsvetozar Biserov

Stooge Sort

  • Ω( n2.71 )
  • Θ( n2.71 )
  • O( n2.71 )
  • space O( log n )

Stooge sort fixes the two ends of a range and then calls itself three times on overlapping slices that each cover two thirds of the range. The overlap is what makes it correct. It is also why it is so slow: it performs the same fixed number of comparisons on a sorted array as on a reversed one, and it never learns that the work is already done.

Array
Comparator strip Index down, time to the right
100 elements
×20 steps/frame

Stooge Sort is limited to 150 elements: on average it takes Θ( n2.71 ) steps.

Comparisons 1
—
Reads 2
—
Writes 2
—
Swaps 3
—
Real time 4
—
Visual time 4
—
  1. Comparisons count only element-against-element checks; loop and index checks are never counted.
  2. Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
  3. A swap counts once, as a swap. It is never also counted as a read or a write.
  4. Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.

How stooge sort works

The call on 5, 2, 6, 1, 4, 3 starts by comparing the two ends, 5 and 3. They are out of order, so it swaps them and the array becomes 3, 2, 6, 1, 4, 5. The range has six elements, so a third is 2, and the three recursive calls cover positions 0 to 3, then 2 to 5, then 0 to 3 again, each four elements wide.

The first call on 3, 2, 6, 1 compares its own ends and swaps 3 with 1, giving 1, 2, 6, 3, 4, 5. It then recurses into slices of three, and most of those find nothing to do. The first real work comes when a slice looks at 6 and 3 side by side and swaps them: 1, 2, 3, 6, 4, 5. The second call, on positions 2 to 5, spots the pair 6 and 4 and swaps them to 1, 2, 3, 4, 6, 5. Inside the same call, a slice over positions 4 to 5 finds 6 before 5 and swaps once more, giving 1, 2, 3, 4, 5, 6.

That is five swaps in total, and the table ends there. The run is not finished, though. The closing call over positions 0 to 3 still has to confirm that nothing remains to fix, and it does so with its own batch of comparisons. The whole run costs 40 comparisons and 5 swaps. The comparisons would be exactly the same for an already sorted six-element array, which would need no swaps at all.

Stooge Sort on 5 2 6 1 4 3, one swap or write at a time. Indexes count from 0.
Step Change Array after
1 Swap indexes 0 and 5 3 2 6 1 4 5
2 Swap indexes 0 and 3 1 2 6 3 4 5
3 Swap indexes 2 and 3 1 2 3 6 4 5
4 Swap indexes 3 and 4 1 2 3 4 6 5
5 Swap indexes 4 and 5 1 2 3 4 5 6

All 5 changes are shown.

Pseudocode

stooge(lo, hi)
  if a[hi] < a[lo]
    swap a[lo], a[hi]
  if hi - lo + 1 > 2
    third = floor((hi - lo + 1) / 3)
    stooge(lo, hi - third)
    stooge(lo + third, hi)
    stooge(lo, hi - third)

Why these complexities

Every call does one comparison and then, for any range longer than two elements, makes three recursive calls on a range of size n minus floor(n / 3), which is about two thirds of n. That gives the recurrence T(n) = 3 T(2n / 3) + 1, whose solution grows as n raised to log 3 over log 1.5, roughly n^2.71. Because the recursion never looks at the data to decide whether to stop, best, average and worst case are all the same. Measured here, the call counts are 40, 364, 3280 and 29524 for 6, 12, 24 and 48 elements, whether the input is sorted or reversed. Small sizes grow by about nine times per doubling because the depth increases in whole levels; the long-run factor settles near 2^2.71, about 6.5.

Only the swaps depend on the input. A sorted array has none, a reversed array of six has seven, and at 48 elements the reversed case still needs fewer than a thousand swaps against nearly thirty thousand comparisons. The cost is the repeated verification, not the repairs. This makes stooge sort slower than bubble sort, which is only quadratic.

Space is the recursion stack. Each level shrinks the range to two thirds, so the depth is about log base 1.5 of n, which is O(log n), and nothing else is allocated. The sort is in place. It is not stable: a swap between the two ends of a range can jump an element over equal neighbours, so [2a, 2b, 1] becomes [1, 2b, 2a] and the two equal values change order.

Measured on this site

Exact counts from this site's own engine for 32 elements, using the same rules as the visualiser.

Input Comparisons Writes Swaps
Random 9,841 0 251
Already sorted 9,841 0 0
Reversed 9,841 0 396
Few distinct values 9,841 0 78

When to use it

Use it only to study recursion and how a correct algorithm can still be very slow. It has no practical use.

Frequently asked questions

Why does stooge sort work?

Sorting the first two thirds and then the last two thirds brings the largest elements to the end. A third pass over the first two thirds fixes the rest.

How slow is it?

About O(n^2.71), slower than bubble sort.

Why is the size limited here?

The step count grows so fast that larger arrays would produce an animation that never ends in a useful time.

Does the input order change how long stooge sort takes?

Not the comparisons. The algorithm always makes the same calls for a given length, so 6 elements cost 40 comparisons whether they arrive sorted or reversed, and 48 elements cost 29524. Only the swap count moves, from zero for sorted input to 958 for reversed input at 48 elements. Stooge sort is therefore not adaptive, and the animation takes about as long for data that is already sorted as for the worst data.

Why thirds? Would halves work?

Two halves would not. After sorting the first half, the largest element can sit at the end of that half, which the second call on the last half never reaches. Two overlapping two-thirds slices guarantee that the biggest third of the elements passes through the overlap and ends up in the last third. A final pass over the first two thirds then sorts what is left. Two thirds is the overlap this three-call scheme needs to be provably correct.

Background

The name comes from the Three Stooges, the comedy trio, and the algorithm is best known as a problem in Cormen, Leiserson, Rivest and Stein, where fictional Professors Howard, Fine and Howard propose it and the reader is asked to prove it correct and analyse it. Who first wrote it down is not clear, and it is usually presented as folklore.

Its place in the literature is as a teaching example of how correctness and efficiency are separate questions. Broder and Stolfi's tongue-in-cheek work on pessimal algorithms builds the same idea into a discipline, producing algorithms such as slowsort that are slower still while remaining provably correct. Stooge sort sits at the mild end of that family.

References

  • Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms, 2nd edition, problem 7-3 (Stooge sort)
  • Broder, Stolfi, Pessimal algorithms and simplexity analysis, ACM SIGACT News, 1984

Compare with

Algorithm Average time Worst time
Bogo Sort Θ( n · n! ) unbounded
Big O notation
Best time Ω( n2.71 ) poor
Average time Θ( n2.71 ) poor
Worst time O( n2.71 ) poor
Worst space O( log n ) good

Properties

Stable
No
In place
Yes
Adaptive
No
Comparison-based
Yes