Guest User

Untitled

a guest
Feb 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.00 KB | None | 0 0
  1. program HeatSort;
  2. {$R+}
  3. {$O-}
  4. {$APPTYPE CONSOLE}
  5. uses
  6.   SysUtils;
  7. var n,i,hs,s,y:longint;
  8.     h,a: packed array[1..100000] of integer;
  9. procedure siftup(i:longint);
  10. begin
  11.   if i=1 then
  12.     exit;
  13.   if h[i]<h[i div 2] then
  14.     begin
  15.       y:=h[i];
  16.       h[i]:=h[i div 2];
  17.       h[i div 2]:=y;
  18.       siftup(i div 2);
  19.     end;
  20. end;
  21. procedure Siftdown(i:longint);
  22. begin
  23.   if (2*i<=hs) and (h[2*i]<h[i]) then
  24.     s:=2*i else
  25.       s:=i;
  26.   if (2*i+1<=hs) and (h[2*i+1]<h[s]) then
  27.     s:=2*i+1;
  28.   if s<>i then
  29.     begin
  30.       y:=h[s];
  31.       h[s]:=h[i];
  32.       h[i]:=y;
  33.       siftdown(s);
  34.     end;
  35. end;
  36. procedure Delete(i:longint);
  37. begin
  38.   h[i]:=h[hs];
  39.   dec(hs);
  40.   siftdown(i);
  41. end;
  42. procedure Add(x:longint);
  43. begin
  44.   inc(hs);
  45.   h[hs]:=x;
  46.   siftup(hs);
  47. end;
  48. begin
  49.   reset(input,'input.txt');
  50.   rewrite(output,'output.txt');
  51.   read(n);
  52.   hs:=0;
  53.   for i := 1 to n do
  54.     read(a[i]);
  55.   for i := 1 to n do
  56.     add(a[i]);
  57.   for i := 1 to n do
  58.     begin
  59.       a[i]:=h[1];
  60.       Delete(1);
  61.     end;
  62.   for i := 1 to n do
  63.     write(a[i],' ');
  64. end.
Add Comment
Please, Sign In to add comment