Anonymous user
Sorting algorithms/Strand sort: Difference between revisions
Updated second D entry
(→Faster version using slices: we're already using the identifier "result" in the enclosing function, better not use it also in the nested function, "res" is clear enough) |
(Updated second D entry) |
||
Line 283:
<lang d>import std.stdio, std.array;
T[] strandSort(T)(
static T[] merge(const(T)[] left, const(T)[] right) pure nothrow {
T[] res;
while (!left.empty && !right.empty) {
Line 302:
auto sorted = list[0 .. 1];
list.popFront;
foreach (const item; list)
(sorted.back <= item ? sorted : leftover) ~= item;
result = merge(sorted, result);
Line 313:
void main() {
arr.strandSort.writeln;
}</lang>
|