Sleep Sort with POE

Sleep sort is described in this silly 4chan thread. I don't guarantee work safety.

It's essentially an insertion sort into time itself. A timer is created for each numeric value to sort, and the order in which they occur determines the outcome.

I've implemented a parallel version using POE.

#!perl

use warnings;
use strict;
use POE;

POE::Session->create(
    inline_states => {
        _start => sub {
            $_[KERNEL]->delay_add(ding => $_, $_) foreach @ARGV;
        },
        ding => sub {
            print "$_[ARG0]\n";
        }
    }
);

POE::Kernel->run();

And some sample output, using time(1) to show that it sleeps about as long as the largest value to sort.

% time perl sleep-sort.pl 9 4 2 8 3 7 1 0 4 2 8 
0
1
2
2
3
4
4
7
8
8
9
real   9.11
user   0.08
sys    0.02

1 Comment

The 4chan bash version is parallel as well - that's how this "algorithm" works :)

Leave a comment

About Rocco Caputo

user-pic Among other things I write software, a lot of which is in Perl.