日本語

JOKE SORTS

Sleep Sort, visualised

Sleep sort is a joke algorithm that waits for a time proportional to each value before outputting it, so smaller values (shorter waits) come out first.

Run Sleep Sort in the visualizer →

Opens the Sorting Cinema visualizer with 16 elements in English.

How it works

  1. The original idea: start a timer for every element at once, each waiting in proportion to its value.
  2. Output values in the order the timers wake up; smaller values come first.
  3. This app's implementation: a simulation that uses the value-sorted order and places values from the left one by one, each after a delay proportional to its value.

Complexity and stability

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

This app computes the order first and then animates the delays — a simulation — so this page gives no complexity figures.

Source of facts: Complexity follows the learning guide's comparison table and text, and this app's implementation.

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
1Value 1 wakes up and goes to [0][1, 2, 4, 1, 3]
2Value 2 wakes up and goes to [1][1, 2, 4, 1, 3]
3Value 3 wakes up and goes to [2][1, 2, 3, 1, 3]
4Value 4 wakes up and goes to [3][1, 2, 3, 4, 3]
5Value 5 wakes up and goes to [4][1, 2, 3, 4, 5]

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

Suitable inputs and caveats

  • A fun teaching example of “sorting with time”.
  • As the learning guide notes, this app simulates the delays based on the value-sorted order.
  • In this app the delay is proportional to the value, so larger values wait longer before being placed.

FAQ

Does this app really sort by sleeping?

No. It uses the value-sorted order and places each value after a delay proportional to it — a simulation.

Is sleep sort practical?

It is treated as a joke sort, and this app includes it as a teaching simulation. Larger values mean longer waits.