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-20 14:43

Ok, I've been thinking about this sleep sort thing for a few days now.  Thanks OP for nerd-sniping me.  First off, the command `usleep` should be used in place of `sleep` for a significant speedup.  But along this same vein, what we really want is to get to the granularity of the number of clock cycles it takes to print a number.  Furthermore, if we alter this for a real-time scheduler, then we can just schedule each print with a priority of i times a constant (the amount of time it would take to print i).  The scary part is that this would run in O(n) time -- which is supposed to be impossible.

Newer Posts