Bash: Sleepsort Algorithm
Some code that a friend showed to me was this:
#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait
Example:
./sleepsort 9 3 7 1 3 2 4 7 4 6
He called it "sleepsort". And somehow, it really works. Not the fastest, but it works.
The results seem to be amusingly non-deterministic with non-numeric input:
$ for i in 1 2 3 4 5; do ./sleepsort a 1 c 3 2 b | fmt; done
a c b 1 2 3
c a b 1 2 3
a b c 1 2 3
a c b 1 2 3
a c b 1 2 3
$
What do you mean, "not the fastest"? This algorithm actually achieves O(n) complexity ;)
Yeah, good find columbo ;)