Inspired by Implement Sleep Sort:
Sleep Sort is an integer sorting algorithm I found on the Internet. It opens an output stream, and for each input numbers in parallel, delay for the number seconds and output that number. Because of the delays, the highest number will be outputted last.
I would like to know if it is possible to implement this in Mathematica. As a version 7 user I would of course like to see a solution that works there, but I think that RunScheduledTask
may be needed to accomplish this.
Answer
Here is an implementation that uses the scheduled tasks in version 8:
sleepSort[l_, t_:0.1] :=
Scan[RunScheduledTask[Print[#];RemoveScheduledTask[$ScheduledTask], {t #}]&, l]
In a notebook environment, output will appear in the messages window. The time interval is specified as the optional second argument, defaulting to 1/10th of a second (I'm impatient). Don't set it too low, though -- the vagaries of the timing might produce results out of order.
Comments
Post a Comment