Procedure HeapSort (Var Data : ArrayType;
NumElements : Integer );
(* Sort the first NumElements values of array Data in ascending order, *)
(* using the heapsort algorithm . *)
Var
NodeIndex : Integer;
Begin (* HeapSort *)
(* Sort the elements in the heap by swapping the root *)
(* (current largest) value with the last unsorted value *)
(* then reheaping the remaining part of the array . *)
For NodeIndex := NumElements Downto 2 Do
Begin
Swap (Data [1] , Data [NodeIndex] );
ReheapDown (Data, 1, NodeIndex - 1)
End (* For loop *)
End; (* HeapSort *)