Sorting algorithms/Heapsort: Difference between revisions

no edit summary
(Added Kotlin)
No edit summary
Line 3,616:
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#heapSort]
 
=={{header|SequenceL}}==
<lang sequenceL>
import <Utilities/Sequence.sl>;
 
TUPLE<T> ::= (A: T, B: T);
 
heapSort(x(1)) :=
let
heapified := heapify(x, (size(x) - 2) / 2 + 1);
in
sortLoop(heapified, size(heapified));
 
heapify(x(1), i) :=
x when i <= 0 else
heapify(siftDown(x, i, size(x)), i - 1);
 
sortLoop(x(1), i) :=
x when i <= 2 else
sortLoop( siftDown(swap(x, 1, i), 1, i - 1), i - 1);
 
siftDown(x(1), start, end) :=
let
child := start * 2;
child1 := child + 1 when child + 1 <= end and x[child] < x[child + 1] else child;
in
x when child >= end else
x when x[start] >= x[child1] else
siftDown(swap(x, child1, start), child1, end);
 
swap(list(1), i, j) :=
let
vals := (A: list[i], B: list[j]);
in
setElementAt(setElementAt(list, i, vals.B), j, vals.A);
</lang>
=={{header|Sidef}}==
<lang ruby>func sift_down(a, start, end) {
Anonymous user