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
- Shuffle generates a new random permutation of numbers.
- Adjust the delay factor slider to scale the sleep time.
- Watch each bar
wake up
in order.
Source code lives in games/js/demos.js
. Please don't use Sleep Sort in production.