Heapsort
 

procedure sort( var r : ArrayToSort; lo, up : integer );

var i : integer;
tempr : ArrayEntry;
begin
{*** construct heap ***}
for i := (up div 2) downto 2 do siftup(r,i,up);
{*** repeatedly extract maximum ***}
for i := up downto 2 do begin
siftup(r,1,i);
tempr := r[1];
r[1] := r[i];
r[i] := tempr
end
end;


sort( r, lo, up )
ArrayToSort r;
int lo, up;

{int i;
/*** construct heap ***/
for ( i=up/2; i>1; i-- ) siftup(r,i,up);
/*** repeatedly extract maximum ***/
for ( i=up; i>1; i-- ) {
siftup(r,1,i);
exchange( r, 1, i );
}
};

(c) Shilpa Sayura Foundation 2006-2017