« 2011年03月 | メイン | 2011年06月 »

2011年05月 アーカイブ

2011年05月20日

Sleep Sort の C#/.NET4 版

※ 参考: Sleep sortの各言語での実装まとめ

// Sleep Sort C#/.NET4 版

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

public static class SleepSort
{
    public static IEnumerable<T> Sort<T>(this IEnumerable<T> collection, Func<T, int> convert)
    {
        var queue = new ConcurrentQueue<T>();
        collection.AsParallel().ForAll(
            value => {
                Thread.Sleep(convert(value));
                queue.Enqueue(value);
            }
        );
        return queue;
    }
}  

static class Program
{
    static void Main()
    {
        var sortedCollection = new[] { 30, 200, 70, 400, 800, 35 }.Sort(x => x);
        foreach (var item in sortedCollection)
            Console.WriteLine(item);
    }
}

About 2011年05月

2011年05月にブログ「プログラミング C# - 翔ソフトウェア (Sho's)」に投稿されたすべてのエントリーです。過去のものから新しいものへ順番に並んでいます。

前のアーカイブは2011年03月です。

次のアーカイブは2011年06月です。

他にも多くのエントリーがあります。メインページアーカイブページも見てください。

Powered by
Movable Type 3.35