Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program HeatSort;
- {$R+}
- {$O-}
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- var n,i,hs,s,y:longint;
- h,a: packed array[1..100000] of integer;
- procedure siftup(i:longint);
- begin
- if i=1 then
- exit;
- if h[i]<h[i div 2] then
- begin
- y:=h[i];
- h[i]:=h[i div 2];
- h[i div 2]:=y;
- siftup(i div 2);
- end;
- end;
- procedure Siftdown(i:longint);
- begin
- if (2*i<=hs) and (h[2*i]<h[i]) then
- s:=2*i else
- s:=i;
- if (2*i+1<=hs) and (h[2*i+1]<h[s]) then
- s:=2*i+1;
- if s<>i then
- begin
- y:=h[s];
- h[s]:=h[i];
- h[i]:=y;
- siftdown(s);
- end;
- end;
- procedure Delete(i:longint);
- begin
- h[i]:=h[hs];
- dec(hs);
- siftdown(i);
- end;
- procedure Add(x:longint);
- begin
- inc(hs);
- h[hs]:=x;
- siftup(hs);
- end;
- begin
- reset(input,'input.txt');
- rewrite(output,'output.txt');
- read(n);
- hs:=0;
- for i := 1 to n do
- read(a[i]);
- for i := 1 to n do
- add(a[i]);
- for i := 1 to n do
- begin
- a[i]:=h[1];
- Delete(1);
- end;
- for i := 1 to n do
- write(a[i],' ');
- end.
Add Comment
Please, Sign In to add comment