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: Bill 2011-06-16 9:59

I hate to rain on every ones parade, but this algorithm is only low order if you ignore kernel scheduling efficiencies.  As you add more processes the kernel will have to do more work to correctly order the invocations.  I do not know what the typical scheduler efficiency is, but I would not be surprised if it is O(n^2) or worse.

Newer Posts