日本語

UNUSUAL STRUCTURES

Stooge Sort, visualised

Stooge sort recursively sorts the first two-thirds, the last two-thirds, and the first two-thirds again — a deliberately redundant procedure.

Run Stooge Sort in the visualizer →

Opens the Sorting Cinema visualizer with 20 elements in English.

How it works

  1. Compare the two ends of the range and swap if the left end is larger.
  2. If the range has three or more elements, let t be one third of its length (rounded down).
  3. Recursively sort the front part (all but the last t), the back part (all but the first t), and the front part again.

Complexity and stability

Best
O(n^2.71)
Average
O(n^2.71)
Worst
O(n^2.71)
Extra space
O(log n)
Stability
Usually unstable

From the recurrence T(n) = 3T(2n/3) + O(1), it is O(n^(log 3 / log 1.5)) ≈ O(n^2.71) for any input. Recursion depth is O(log n).

Source of facts: Complexity is derived from the steps of this app's implementation (src/SortingCinema.jsx).

Trace on a small array

The first operations on the input [5, 2, 4, 1, 3], following the same steps as the app.

#OperationArray after
1Compare [0]=5 and [4]=3[5, 2, 4, 1, 3]
2Swap [0]=5 and [4]=3[3, 2, 4, 1, 5]
3Compare [0]=3 and [3]=1[3, 2, 4, 1, 5]
4Swap [0]=3 and [3]=1[1, 2, 4, 3, 5]
5Compare [0]=1 and [2]=4[1, 2, 4, 3, 5]
6Compare [0]=1 and [1]=2[1, 2, 4, 3, 5]
7Compare [1]=2 and [2]=4[1, 2, 4, 3, 5]
8Compare [0]=1 and [1]=2[1, 2, 4, 3, 5]

…and so on. In this trace, 43 compare / swap / write operations in total produce [1, 2, 3, 4, 5].

Suitable inputs and caveats

  • Useful for seeing how inefficient it is to re-sort the same range repeatedly.
  • This app automatically reduces the array to 20 elements for Play and Step.
  • It swaps distant ends, so it is usually unstable.

FAQ

Why is it limited to 20 elements?

Its O(n^2.71) cost means large arrays take far too long. This app reduces the array to 20 elements for Play and Step.

Is stooge sort practical?

No. It re-sorts overlapping ranges, so it costs more than O(n²) sorts.