Sleep Sort

Sorting by setting alarm clocks — concurrency abuse at its finest.

How It Works

For each integer n in the list we spawn a timer that sleeps for n × k milliseconds (k is a global scaling factor). When the timer wakes it outputs n. Because smaller numbers have shorter naps they are printed first, yielding a sorted sequence.

Complexity

In theory: O(n) time (wall-clock) and O(n) space for n timers. In practice: heavily dependent on the operating system's scheduler and timer resolution. Definitely not stable, reliable or advisable.

Gallery Controls

Source code lives in games/js/demos.js. Please don't use Sleep Sort in production.

← Back to gallery