Implement Sleep Sort
C, 127 characters, a rather obvious solution:
main(int c,char**v){
#pragma omp parallel for num_threads(c)
for(int i=1;i<c;++i){int x=atoi(v[i]);sleep(x);printf("%d ",x);}}
(Compiled with gcc -std=c99 -fopenmp sort.c
and ignoring all warnings.)
A kind of lame Perl attempt, 59 55 52 38 32 characters:
map{fork||exit print sleep$_}@a
Barebones: 25 Characters:
...if you don't mind the sort results as die output:
map{fork||die sleep$_}@a
With all the trimmings:
(for maximum challenge compliance, 44 characters)
sub t{map{fork||exit print sleep$_}@_;wait}
If you let perl do the wait for you, 39 characters:
sub t{map{fork||exit print sleep$_}@_}
And again, if you don't mind die()
, 32 characters...
sub t{map{fork||die sleep$_}@_}
Note that in Perl 6, or when the 'say' feature is declared, it is possible to replace the print
function with say
, saving a character in each instance. Obviously since die
both terminates the forked process and writes the output, it remains the shortest solution.
Ruby 1.9, 32 characters
As a function:
s=->a{a.map{|i|fork{p sleep i}}}
If we can just use a predefined variable, it reduces to 25 characters:
a.map{|i|fork{p sleep i}}