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 classSleepSort
{
public staticIEnumerable<T> Sort<T>(thisIEnumerable<T> collection, Func<T, int> convert)
{
var queue = newConcurrentQueue<T>();
collection.AsParallel().ForAll(
value => {
Thread.Sleep(convert(value));
queue.Enqueue(value);
}
);
return queue;
}
}
static classProgram
{
static void Main()
{
var sortedCollection = new[] { 30, 200, 70, 400, 800, 35 }.Sort(x => x);
foreach (var item in sortedCollection)
Console.WriteLine(item);
}
}

.NET

Posted by Fujiwo