all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,13 @@
import std.stdio, std.algorithm, std.array, std.range;
T[] mergeSorted(T)(in T[] D) /*pure nothrow*/ {
if (D.length < 2)
return D.dup;
return [D[0 .. $ / 2].mergeSorted(), D[$ / 2 .. $].mergeSorted()]
.nWayUnion().array();
}
void main() {
auto a = [3, 4, 2, 5, 1, 6];
writeln(a.mergeSorted());
}

View file

@ -0,0 +1,19 @@
import std.stdio, std.algorithm, core.stdc.stdlib, std.exception,
std.range;
void mergeSort(T)(T[] data) if (hasSwappableElements!(typeof(data))) {
immutable L = data.length;
if (L < 2) return;
T* ptr = cast(T*)alloca(L * T.sizeof);
enforce(ptr != null);
ptr[0 .. L] = data[];
mergeSort(ptr[0 .. L/2]);
mergeSort(ptr[L/2 .. L]);
[ptr[0 .. L/2], ptr[L/2 .. L]].nWayUnion().copy(data);
}
void main() {
auto a = [3, 4, 2, 5, 1, 6];
a.mergeSort();
writeln(a);
}