Zlúčenie usporiadaných postupností

31 okt

Jedná sa o zlúčenie dvoch postupností, kde každá je usporiadaná a má po 10 prvkov.

program zlucenie4;

uses crt;
var x,y,z,i:integer;
a,b,c:array[1..30]of integer;
begin
randomize;
a[1]:=random(5);
b[1]:=random(5);

for i:= 2 to 10 do
begin
a[i]:=a[i-1]+random(5);
b[i]:=b[i-1]+random(5);
end;
write(‚a: ‚);
for i:= 1 to 10 do
write(a[i],‘ ‚);
writeln;
write(‚b: ‚);
for i:= 1 to 10 do
write(b[i],‘ ‚);
writeln;
x:=1;
y:=1;
z:=1;
while (y<11) and (x<11) do
begin
if a[x]>b[y] then begin
c[z]:=b[y];
inc(z);
if y<11 then inc(y);
end
else begin
c[z]:=a[x];
inc(z);
if x<11 then inc(x);
end;

end;
writeln;
if x<y then begin
for i:= x to 10 do
begin
c[z]:=a[i];
inc(z)
end;
end
else begin
for i:= y to 10 do
begin
c[z]:=b[i];
inc(z)
end;
end;

for i:= 1 to 20 do
write(c[i],‘ ‚);

readln;
end.