The 'Sleep Sort' meme

People keep pointing me at Sleep Sort in email and IRL; it is apparently a popular topic right now. To Sleep Sort an array of integers, you spawn one process per array element. Each process sleeps t seconds, where t is the value of its array element, and then outputs t If there are duplicate values in the array, you will probably need some kind of locking so that the digits of the duplicates aren't interleaved. If you have negative integers, you will want to pick a positive additive constant that makes them all non-negative; add it on at the beginning and pull it off at the end. Indeed, you probably want to bias the values so that the lowest value is 0, so that you don't have to wait to start outputting. You also probably want to pick your time scale so that rather than seconds you are working in milliseconds or something; whatever the smallest time scale that has reliable delay times is.

The algorithm is O(n) for an array of n elements whose contents are a permutation of 1..n. Of course, in this case, there's an even simpler O(n) algorithm with better constant factors: ignore the input and output 1..n.

Sleep Sort is essentially a weird implementation of a Radix Sort, specifically an MSD Radix Sort with the radix equal to the largest value. The good thing about a full-on Radix Sort is that, unlike Sleep Sort, it can cope reasonably with widely varying gaps between values. The bad thing about a full-on Radix Sort is that, like Sleep Sort, it can only sort integers or things very like them. Just having a total order isn't enough, since Radix Sort is positional rather than comparison based. All in all, Radix Sort is better than Sleep Sort in any situation I can imagine.

The article linked above for Sleep Sort may well be the original. Finding it on 4chan probably shouldn't increase your hope that it will be something well-thought-out and useful. Sleep Sort is a cute idea, but not a particularly good sorting algorithm. (B)