326 points · by olalonde · 11y ago · 68 comments
First of all, there are quite a few linear time sorting algorithms. Radix, pigeonhole, and counting sort are all linear time sorting algorithms. The popular result that any comparison-based sorting algorithm works in O(n log n) applies _specifically_ to comparison-based sorting algorithms, and not those like the above. So, even if you ignore the underlying mechanics of the OS scheduler and assume sleep works "perfectly", the result would not that unusual.
Second off, in the comments there appears to be considerable time worrying about the technicalities of the scheduler, the nondeterministic nature of sleep, and so on, and whether this really implies linear time bound. IMHO these are not worth worrying about because we already have linear time sorting algorithms. It's fine to assume the scheduler adds no significant asymptotic cost here, even if we know differently.
Third off, remember that all of these sorting bounds assume machines of the Von Neumann architecture. In particular, this model assumes constant time memory and constant time comparison operations. In cases where you're comparing really big numbers, these bounds get worse. This is easy to forget, but worth remembering just since we're on the subject anyway.
But it is not linear time, we shouldn’t let the existence of other algorithms make us confused about proper analysis of this one.
The call to `sleep(n)` is not a trivial operation, it inserts our task into a priority queue, so we are piggybacking on the OS’ support for sorting tasks based on when they need to be woken up, and thus hide the actual complexity behind the call to `sleep` (which we call `n` times, so if it’s `lg(n)` then the time complexity of the entire algorithm is `O(n lg n)`).
This is directly related to my third point. Traditional analysis of sorting algorithms assumes the Von Neumann architecture, and in particular, it assumes constant-time comparisons.
E.g.
Intput, T: array of n elements, m: number of distinct elements.
for i in 1..m
for j in 1..n
if( T[j] == i ) print T[j];
This is O(n * m) which is O(n) since m is a constant.One reasonable assumption is of course to assume that sleep works as it does on virtually all modern OSs, in which case, you're correct, but that is certainly not the only way it can be. You could easily imagine a specialized SortOS that executes one thread on one process and outputs messages at a specifically scheduled time after a program begins executing, in which case the result would be radix sort with extraneous waiting, rather than priority heap sort with extraneous waiting.
Of course, making lots of system calls to create processes and store them in your operating system's process table almost certainly invokes operations with greater than O(n) asymptotic complexity, but this complexity is still unrelated to max(input).
https://web.archive.org/web/20110622073615/http://en.wikiped...
Reading through Wikipedia's existing pages for other esoteric sorts makes me laugh as I did when reading Hitchhiker's Guide to the Galaxy's various entries for scientific theories (e.g. Bistromathics)...There's Bogosort, Stooge sort, American flag sort, and my favorite, "Gnome sort", named thusly because "that is 'how a gnome sorts a line of flower pots'" http://en.wikipedia.org/wiki/Gnome_sort
1) Shuffle the list.
2) If it's sorted, halt.
3) If it's not sorted, go to step 1.
and the incredibly efficient Quantum Bogosort: 1) Shuffle the list.
2) If it's sorted, halt.
3) If it's not sorted, destroy the Universe.
According to the Many Worlds interpretation, step (1) creates multiple Universes, each with a different ordering of the list. Step (3) ensures that, by the anthropic principle, we must be in a Universe where the list was sorted correctly (otherwise we wouldn't be around to observe anything).I just found out that the WP page for the hornet archive, one of the most important focal points of the international demoscene for a decade, was deleted because it was't "notable".
So frustrating.
I heartily disagree with all the attempts to downplay the brilliance of the sleep sort algorithm. Many of you have missed the important point that while traditional sorting algorithms can only utilize one core, sleep sort has the capacity to use the full power of a massively parallel execution environment.
Given that you need nearly no computing in each of the threads, you can implement them using low-power CPUs, so this is in fact a GREEN COMPUTING algorithm.
Oh, and did I mention that the algorithm can also run inside a cloud...?
Sure, you're a genius!
[1] https://www.kernel.org/doc/Documentation/timers/hrtimers.txt
https://github.com/majek/fluxcapacitor#basic-examples
$ ./fluxcapacitor examples/sleep_sort.sh 1 4 20 3 55
A paper is currently making the rounds, would love to hear feedback:
https://www.dropbox.com/s/zy1gx1azfvvqm6i/NetSciComm%20versi...
There's a reason 4chan should be considered a national...maybe not treasure, but it's pretty awesome.
Pretty sure it's been posted a couple other times, but I'm on a tablet at the moment.
http://perl6advent.wordpress.com/2014/12/23/webscale-sorting...
#include <stdio.h>
#include <windows.h>
#include <process.h>
void sleep_on_it(void* param)
{
int n = *(int*)param;
Sleep(n);
printf("%d ", n);
}
int main(int argc, char **argv)
{
int ar[argc];
HANDLE threads[argc-1];
for (int i = 1; i < argc; i++){
ar[i] = atoi(argv[i]);
threads[i-1] = (HANDLE)_beginthread(&sleep_on_it, 0, &(ar[i]));
}
WaitForMultipleObjects(argc-1, threads, TRUE, INFINITE);
}
Compile with gcc -std=c99 (because of VLA's it won't work in Visual Studio).Example:
D:>sleepsort 1000 9 2 150 1 7 17 624
1 2 7 9 17 150 624 1000
var input = [5, 3, 6, 3, 6, 3, 1, 4, 7];
var timeAxis = [];
// Put every element to the corresponding time point.
// Time point would hold an array, as we can have
// equal elements.
for (var i = 0; i < input.length; i++) {
elem = input[i];
timeAxis[elem] = [elem].concat(timeAxis[elem] || []);
}
// skip all empty (UNDEFINED) time points
// and contact all the arrays:
var sorted = timeAxis.reduce(function (accum, cur) {
return cur ? accum.concat(cur) : accum},
[]);
console.log(sorted)I'm sad nobody has implemented my idea, rock, paper, scissors sort.
http://js.do/code/48526 (Optimized milliseconds). :)
I'm not storing key,value pairs for elements that don't exist (not sure what the Perl code's doing), so it shouldn't be too bad in terms of space used.
ruby -e '[3,1,2].each{|n| system("sleep #{n} && echo #{n} &")}'