Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Genius sorting algorithm: Sleep sort

Name: Anonymous 2011-01-20 12:22

Man, am I a genius. Check out this sorting algorithm I just invented.


#!/bin/bash
function f() {
    sleep "$1"
    echo "$1"
}
while [ -n "$1" ]
do
    f "$1" &
    shift
done
wait


example usage:
./sleepsort.bash 5 3 6 3 6 3 1 4 7

Name: Anonymous 2011-03-31 10:48

Actual Perl implementation. Forking in Perl is awkward to say the least.


#!/usr/bin/perl                                                                
sub f {
  sleep $_[0];
  print "$_[0]\n";
}

for (0..$#ARGV) {
  $pid = fork();
  if ($pid) {
    # parent                                                                   
  push(@childs, $pid);
}
  if ($pid == 0) {
    # child                                                                    
    &f(@ARGV[$_]);
    exit(0);
  }
}
foreach (@childs) {
waitpid($_, 0);
}



time perl sleepsort.pl 3 1 8 2 9 5 4
1
2
3
4
5
8
9

real    0m9.007s
user    0m0.008s
sys    0m0.000s

Terribly inaccurate with anything other than integers, but you can give arguments like 0.003 "sqrt(2)" 10e-3, for what it's worth.

Name: Anonymous 2011-03-31 20:58

way to ruin a good python thread

Newer Posts