Pagini recente » Cod sursa (job #114582) | Rating Vlad Ilie (ili226) | Cod sursa (job #1896570) | Cod sursa (job #743231) | Cod sursa (job #1224232)
program mergesort;
var a,c:array[1..500000]of longint;
n,i:longint;
procedure interclaseaza(s,m,d:longint);
var k,i,j:longint;
begin
i:=s;j:=m+1;k:=0;
while (i<=m)and(j<=d) do
if a[i]<a[j] then begin inc(k);c[k]:=a[i];inc(i); end
else begin inc(k);c[k]:=a[j];inc(j); end;
while (i<=m) do begin inc(k);c[k]:=a[i];inc(i); end;
while (j<=d) do begin inc(k);c[k]:=a[j];inc(j); end;
k:=0;
for i:=s to d do
begin
inc(k);a[i]:=c[k];
end;
end;
procedure mergesort(s,d:longint);
var m:longint;
begin
if s<d then
begin
m:=(s+d) div 2;
mergesort(s,m);
mergesort(m+1,d);
interclaseaza(s,m,d);
end;
end;
begin
assign(input,'algsort.in');reset(input);
assign(output,'algsort.out');rewrite(output);
readln(n);
for i:=1 to n do read(a[i]);
mergesort(1,n);
for i:=1 to n do write(a[i],' ');
close(input);close(output);
end.