日本語

JOKE SORTS

Slow Sort, visualised

Slow sort is a joke algorithm that splits the range in half like merge sort but is deliberately designed to take the longest possible route.

Run Slow Sort in the visualizer →

Opens the Sorting Cinema visualizer with 16 elements in English.

How it works

  1. Split the range at the middle and recursively slow-sort each half.
  2. Compare the maximum of the left half (the middle) with the maximum of the right half (the end) and swap the larger one to the end.
  3. Recursively slow-sort the range again without the last element.

Complexity and stability

Best
— (no claim on this page)
Average
— (no claim on this page)
Worst
— (no claim on this page)
Extra space
O(n)
Stability
— (no claim on this page)

Its cost follows T(n) = 2T(n/2) + T(n−1) + O(1), which grows faster than any polynomial. This page does not give a specific Big-O symbol. Recursion depth is up to O(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 [1]=2[5, 2, 4, 1, 3]
2Swap [0]=5 and [1]=2[2, 5, 4, 1, 3]
3Compare [1]=5 and [2]=4[2, 5, 4, 1, 3]
4Swap [1]=5 and [2]=4[2, 4, 5, 1, 3]
5Compare [0]=2 and [1]=4[2, 4, 5, 1, 3]
6Compare [3]=1 and [4]=3[2, 4, 5, 1, 3]
7Compare [2]=5 and [4]=3[2, 4, 5, 1, 3]
8Swap [2]=5 and [4]=3[2, 4, 3, 1, 5]

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

Suitable inputs and caveats

  • An example of “divide and conquer that is slow”, showing heavily repeated recursion.
  • This app automatically reduces the array to 16 elements for Play and Step.
  • Comparisons and swaps explode quickly, so try small sizes.

FAQ

How does slow sort differ from merge sort?

Both recurse on halves, but instead of merging, slow sort moves the maximum to the end and slow-sorts everything else again, so the work explodes.

Why is it limited to 16 elements?

Its cost is enormous, so larger arrays take far too long. This app reduces the array to 16 elements for Play and Step.