Pagini recente » Cod sursa (job #3227092) | Cod sursa (job #1579033) | Cod sursa (job #1218890) | Cod sursa (job #2246880) | Cod sursa (job #342224)
Cod sursa(job #342224)
Program algsort;
var f,g:text; h:array[0..500000]of longint;
i,n,nh:longint;
procedure swap (x,y:longint);
var z:longint;
begin
z:=h[x]; h[x]:=h[y]; h[y]:=z;
end;
procedure upheap (x:longint);
begin
if h[x]<h[x div 2] then begin
swap (x,x div 2);
upheap (x div 2);
end;
end;
procedure downheap (x:longint);
var y:longint;
begin
if 2*x<=nh then begin
y:=x;
if h[y]>h[2*x] then y:=2*x;
if 2*x+1<=nh then if h[y]>h[2*x+1] then y:=2*x+1;
if x<>y then begin
swap (x,y);
downheap (y);
end;
end;
end;
procedure delete;
begin
swap (1,nh);
dec (nh);
downheap (1);
end;
begin
assign (f,'algsort.in'); reset (f);
assign (g,'algsort.out'); rewrite (g);
readln (f,n);
for i:=1 to n do read (f,h[i]);
h[0]:=-1;
nh:=n;
for i:=(nh div 2) downto 1 do downheap (i);
for i:=n downto 2 do delete;
for i:=n downto 2 do write (g,h[i],' ');
writeln (g,h[1]);
close (f); close (g);
end.