Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,12 @@
|
|||
import std.stdio, std.algorithm, std.array, std.range;
|
||||
|
||||
T[] mergeSorted(T)(in T[] D) /*pure nothrow @safe*/ {
|
||||
if (D.length < 2)
|
||||
return D.dup;
|
||||
return [D[0 .. $ / 2].mergeSorted, D[$ / 2 .. $].mergeSorted]
|
||||
.nWayUnion.array;
|
||||
}
|
||||
|
||||
void main() {
|
||||
[3, 4, 2, 5, 1, 6].mergeSorted.writeln;
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue