Sorting algorithms/Heapsort: Difference between revisions
Content added Content deleted
Corpsmoderne (talk | contribs) (→{{header|Haskell}}: add an implementation using only the standard library) |
(Added XPL0 example.) |
||
Line 6,233: | Line 6,233: | ||
As above. |
As above. |
||
</pre> |
</pre> |
||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang "XPL0">proc HeapSort(Array, Size); |
|||
int Array, Size; |
|||
int First, Last, T; |
|||
proc Sift(First, Count); |
|||
int First, Count; |
|||
int Root, Child, T; |
|||
[Root:= First; |
|||
loop [if Root*2 + 1 >= Count then quit; |
|||
Child:= Root*2 + 1; |
|||
if Child < Count-1 and Array(Child) < Array(Child+1) then |
|||
Child:= Child+1; |
|||
if Array(Root) < Array(Child) then |
|||
[T:= Array(Root); Array(Root):= Array(Child); Array(Child):= T; |
|||
Root:= Child; |
|||
] |
|||
else quit; |
|||
]; |
|||
]; |
|||
[First:= (Size-1)/2 - 1; |
|||
Last:= Size-1; |
|||
while First >= 0 do |
|||
[Sift(First, Size-1); |
|||
First:= First-1; |
|||
]; |
|||
while Last > 0 do |
|||
[T:= Array(Last); Array(Last):= Array(0); Array(0):= T; |
|||
Sift(0, Last); |
|||
Last:= Last-1; |
|||
]; |
|||
]; |
|||
int Array, Size, I; |
|||
[Array:= [4, 65, 2, 31, 0, 99, 2, 8, 3, 782, 1]; |
|||
Size:= 11; |
|||
HeapSort(Array, Size); |
|||
for I:= 0, Size-1 do |
|||
[IntOut(0, Array(I)); ChOut(0, ^ )]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
0 1 2 2 3 4 8 31 65 99 782 </pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |