Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var
- heap:array[1..100000] of longint;
- i,j,n,m,t:longint;
- procedure swap(var a,b:longint);
- var temp:longint;
- begin
- temp:=a;
- a:=b;
- b:=temp;
- end;
- procedure build(i:longint);
- var l,r,max:longint;
- begin
- l:=i shl 1;
- r:=l+1;
- max:=i;
- if (l<=n) and (heap[l] > heap[max]) then
- max:=l;
- if (r<=n) and (heap[r] > heap[max]) then
- max:=r;
- if (max <> i) then
- begin
- swap(heap[i],heap[max]);
- build(max);
- end;
- end;
- procedure heapsort(n:longint);
- begin
- swap(heap[1],heap[n]);
- dec(n);
- build(1);
- end;
- Begin
- read(n);
- for i:=1 to n do
- read(heap[i]);
- for i:=n shr 1 downto 1 do
- build(i);
- for i:=1 to n do
- heapsort(n-i+1);
- for i:=1 to n do
- write(heap[i],' ');
- end.
Advertisement
Add Comment
Please, Sign In to add comment