Sorting algorithms/Heapsort: Difference between revisions

Added zkl
(Added zkl)
Line 2,656:
:DelVar L<sub>3</sub>
:Return
 
=={{header|zkl}}==
<lang zkl>fcn heapSort(a){ // in place
n := a.len();
foreach start in ([(n-2)/2 .. 0,-1])
{ siftDown(a, start, n-1) }
foreach end in ([n-1 .. 1,-1]){
a.swap(0, end);
siftDown(a, 0, end-1);
}
a
}
 
fcn siftDown(a, start, end){
while((child := start*2 + 1) <= end){
if(child < end and a[child]<a[child+1]) child+=1;
if(a[start] >= a[child]) return();
a.swap(start, child);
start = child;
}
}</lang>
<lang zkl>heapSort(L(170, 45, 75, -90, -802, 24, 2, 66)).println();
heapSort("this is a test".split("")).println();</lang>
{{out}}
<pre>
L(-802,-90,2,24,45,66,75,170)
L(" "," "," ","a","e","h","i","i","s","s","s","t","t","t")
</pre>
 
{{omit from|GUISS}}
Anonymous user