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:
Anonymous2011-01-20 19:47
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
namespace SleepSort
{
public class SleepSort
{
static void Main(string[] args)
{
var xs = new[] { 11, 215, 12, 1985, 12, 1203, 12, 152 };
var sorted = Sort(xs, x => x);
Console.Write("Sorted:");
foreach (var x in sorted)
Console.Write(" {0}", x);
Console.WriteLine();
Console.ReadKey(true);
}
public static IEnumerable<T> Sort<T>(IEnumerable<T> xs, Func<T, int> conv)
{
const int WEIGHT = 40000;
var ev = new EventWaitHandle(false, EventResetMode.ManualReset);
var q = new Queue<T>();
ParameterizedThreadStart f = x => { ev.WaitOne(); Thread.SpinWait(conv((T)x) * WEIGHT); lock (q) { q.Enqueue((T)x); } };
var ts = xs.Select(x => { var t = new Thread(f); t.Start(x); return t; }).ToArray();
ev.Set();
foreach (var t in ts)
t.Join();
return q;
}
}
}
It's still a bit of a stochastic sort.