Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -1,32 +1,11 @@
|
|||
import std.stdio, std.algorithm;
|
||||
import std.stdio, std.container;
|
||||
|
||||
void inplaceHeapSort(R)(R seq) pure nothrow {
|
||||
static void siftDown(R seq, in size_t start,
|
||||
in size_t end) pure nothrow {
|
||||
for (size_t root = start; root * 2 + 1 <= end; ) {
|
||||
auto child = root * 2 + 1;
|
||||
if (child + 1 <= end && seq[child] < seq[child + 1])
|
||||
child++;
|
||||
if (seq[root] < seq[child]) {
|
||||
swap(seq[root], seq[child]);
|
||||
root = child;
|
||||
} else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (seq.length > 1)
|
||||
foreach_reverse (start; 1 .. (seq.length - 2) / 2 + 2)
|
||||
siftDown(seq, start - 1, seq.length - 1);
|
||||
|
||||
foreach_reverse (end; 1 .. seq.length) {
|
||||
swap(seq[end], seq[0]);
|
||||
siftDown(seq, 0, end - 1);
|
||||
}
|
||||
void heapSort(T)(T[] data) /*pure nothrow*/ {
|
||||
for (auto h = data.heapify; !h.empty; h.removeFront) {}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0];
|
||||
inplaceHeapSort(arr);
|
||||
writeln(arr);
|
||||
auto items = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0];
|
||||
items.heapSort;
|
||||
items.writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,28 @@
|
|||
import std.stdio, std.container;
|
||||
import std.stdio, std.algorithm;
|
||||
|
||||
void inplaceHeapSort(T)(T[] data) {
|
||||
auto h = heapify(data);
|
||||
while (!h.empty)
|
||||
h.removeFront();
|
||||
void inplaceHeapSort(R)(R seq) pure nothrow {
|
||||
static void siftDown(R seq, in size_t start,
|
||||
in size_t end) pure nothrow {
|
||||
for (size_t root = start; root * 2 + 1 <= end; ) {
|
||||
auto child = root * 2 + 1;
|
||||
if (child + 1 <= end && seq[child] < seq[child + 1])
|
||||
child++;
|
||||
if (seq[root] < seq[child]) {
|
||||
swap(seq[root], seq[child]);
|
||||
root = child;
|
||||
} else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (seq.length > 1)
|
||||
foreach_reverse (start; 1 .. (seq.length - 2) / 2 + 2)
|
||||
siftDown(seq, start - 1, seq.length - 1);
|
||||
|
||||
foreach_reverse (end; 1 .. seq.length) {
|
||||
swap(seq[end], seq[0]);
|
||||
siftDown(seq, 0, end - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue