Sleep Sort is a hilarious (to me anyway) joke dressed up as a sorting algorithm.

Here it is in non-obfuscated (if somewhat garbagey) Maple code (need version 15 since it uses ?Threads,Sleep )

SleepSort := proc(L::list(posint),$)
local Lout, p, i;
    Lout := NULL;

    p := proc(n::posint,$)
        Threads:-Sleep(n);
        Lout := Lout, n;
    end proc:

    Threads:-Wait( seq( Threads:-Create( p(i) ), i in L ) );

    return [Lout];

end proc: # SleepSort

Test it out like this:

n := 100;
L := RandomTools:-Generate(list(integer(range=1..10), n));
SleepSort(L);

With some help from James McCarron here is a 91 character version that we posted over at CodeGolf:

M:=():use Threads in p:=proc(n)Sleep(n);:-M:=M,n;end:Wait(map(i->Create(p(i)),L)[])end:[M];

This one needs the list to be sorted assigned to L in order to work.  I am interested if anyone can get it done in fewer than 91 characters.

(Disclaimer: This is a terrible way to try to sort integers; there are lots of things which will cause it not to work properly.  For entertainment purposes only.)


Please Wait...