Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,6 +1,6 @@
import std.stdio, std.container;
void heapSort(T)(T[] data) /*pure nothrow*/ {
void heapSort(T)(T[] data) /*pure nothrow @safe @nogc*/ {
for (auto h = data.heapify; !h.empty; h.removeFront) {}
}

View file

@ -1,8 +1,8 @@
import std.stdio, std.algorithm;
void inplaceHeapSort(R)(R seq) pure nothrow {
void heapSort(R)(R seq) pure nothrow @safe @nogc {
static void siftDown(R seq, in size_t start,
in size_t end) pure nothrow {
in size_t end) pure nothrow @safe @nogc {
for (size_t root = start; root * 2 + 1 <= end; ) {
auto child = root * 2 + 1;
if (child + 1 <= end && seq[child] < seq[child + 1])
@ -16,17 +16,17 @@ void inplaceHeapSort(R)(R seq) pure nothrow {
}
if (seq.length > 1)
foreach_reverse (start; 1 .. (seq.length - 2) / 2 + 2)
foreach_reverse (immutable start; 1 .. (seq.length - 2) / 2 + 2)
siftDown(seq, start - 1, seq.length - 1);
foreach_reverse (end; 1 .. seq.length) {
foreach_reverse (immutable end; 1 .. seq.length) {
swap(seq[end], seq[0]);
siftDown(seq, 0, end - 1);
}
}
void main() {
auto arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0];
inplaceHeapSort(arr);
writeln(arr);
auto data = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0];
data.heapSort;
data.writeln;
}