Pagini recente » Cod sursa (job #397209) | Cod sursa (job #3260551) | Cod sursa (job #2389687) | Cod sursa (job #1662223) | Cod sursa (job #244306)
Cod sursa(job #244306)
type tip=0..2147483648;
var a:array[1..500005] of tip;
m,n,i:longint;
aux:tip;
procedure reconstituire_heap(i:longint);inline;
var s,d,max:longint;
begin
s:=2*i; d:=2*i+1;
max:=i;
if (s<=n)and(a[s]>a[i]) then max:=s;
if (d<=n)and(a[d]>a[max]) then max:=d;
if max<>i then
begin
aux:=a[i]; a[i]:=a[max]; a[max]:=aux;
reconstituire_heap(max);
end;
end;
procedure creare_heap; inline;
begin
for i:=n div 2 downto 1 do reconstituire_heap(i);
end;
procedure heap_sort; inline;
begin
creare_heap; m:=n;
for i:=m downto 2 do
begin
aux:=a[1]; a[1]:=a[i]; a[i]:=aux;
dec(n);
reconstituire_heap(1);
end;
end;
begin
assign(input,'algsort.in'); reset(input);
readln(n); for i:=1 to n do read(a[i]);
close(input);
heap_sort;
assign(output,'algsort.out'); rewrite(output);
for i:=1 to m do write(a[i],' ');
close(output);
end.