Thursday, October 6, 2011

SleepSort

static void SleepSort(int[] args)
{
    int count = 0;
    TimerCallback t = (object o) =>
    {
        Console.WriteLine(o);
        Interlocked.Increment(ref count);
    };

    foreach (int a in args)
        new Timer(t, a, a * 1000, System.Threading.Timeout.Infinite);

    while (count < args.Length)
        Thread.Sleep(1000);
}

static void Main(string[] args)
{
    SleepSort(new int[] {5, 3, 12, 10, 1, 2});
}

1 comment:

Jerry said...

Interesting...1st time seeing this.
But imagine how slow it is for large numbers. Intelligent Design Sort, on the other hand, is also interesting while must faster :)
http://www.dangermouse.net/esoteric/intelligentdesignsort.html