Sorting algorithms/Sleep sort: Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
→‎{{header|D}}: Simplify D sleep sort implementation
Line 255: Line 255:


=={{header|D}}==
=={{header|D}}==
<lang d>
<lang d>import std.stdio, std.conv, std.datetime, std.array, core.thread;
import core.thread, std.concurrency, std.datetime,
std.stdio, std.algorithm, std.conv;


void main(string[] args)
final class SleepSorter: Thread {
{
private immutable uint val;
if (!args.length)
return;


foreach (number; args[1 .. $].map!(to!uint))
this(in uint n) /*pure nothrow @safe*/ {
super(&run);
spawn((uint num) {
val = n;
Thread.sleep(dur!"msecs"(10 * num));
writef("%d ", num);
}
}, number);


private void run() {
thread_joinAll();
Thread.sleep(dur!"msecs"(1000 * val));
writef("%d ", val);
}
}
}


</lang>
void main(in string[] args) {
if (!args.empty)
foreach (const arg; args[1 .. $])
new SleepSorter(arg.to!uint).start;
}</lang>
{{out}}
{{out}}
<pre>sorting_algorithms_sleep_sort 1 6 2 5 3 4
<pre>sorting_algorithms_sleep_sort 1 6 2 5 3 4