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-06-15 20:07

>>153
Let's play some golf! Here's a simplified version of yours that accepts any inputs and doesn't require any functions:

#!/usr/bin/env ruby
ARGV.each { |e| fork { sleep(e.to_f/1000); puts e } }

Also, it's much faster, since it's sleeping milliseconds instead of seconds now. You could probably toss more zeroes in, but I'm not sure if that would actually make a difference past what I've got.

Newer Posts