Pagini recente » Cod sursa (job #716127) | Cod sursa (job #2269493) | Cod sursa (job #1969155) | Clasamentul arhivei de probleme | Cod sursa (job #1122170)
Program merge;
type vector=array[1..500000] of longint;
var
n,i:longint;
a,b:vector;
procedure citire(var n:longint; var a:vector);
var f:text;
i:longint;
begin
assign(f,'algsort.in');
reset(f);
readln(f,n);
for i:=1 to n do read(f,a[i]);
close(f);
end;
procedure sortare(ls,ld:longint);
var m,k,j,i:longint;
begin
m:=(ls+ld) div 2;
if ls<ld then
begin
sortare(ls,m);
sortare(m+1,ld);
i:=ls;
j:=m+1;
k:=ls-1;
while (i<=m) and (j<=ld) do
begin
if a[i]>a[j] then begin inc(k); b[k]:=a[j]; inc(j); end
else begin inc(k); b[k]:=a[i]; inc(i); end;
end;
while (i<=m) do begin inc(k); b[k]:=a[i]; inc(i); end;
while (j<=ld) do begin inc(k); b[k]:=a[j]; inc(j); end;
for k:=ls to ld do a[k]:=b[k];
end;
end;
procedure afisare(n:longint; a:vector);
var i:longint;
q:text;
begin
assign(q,'algsort.out');
rewrite(q);
for i:=1 to n do write(q,a[i],' ');
close(q);
end;
begin
citire(n,a);
sortare(1,n);
afisare(n,a);
end.