---
# System prepended metadata

title: Merge2SortedLists TB P.439
tags: [Pascal Programming D1]

---

###### tags: `Pascal Programming D1`
# Merge2SortedLists TB P.439

program Merge2SortedLists;
const
     numA=4;
     numB=3;
     numC=numA+numB;
type
    ArrayTypeA=array[1..numA] of integer;
    ArrayTypeB=array[1..numB] of integer;
    ArrayTypeC=array[1..numC] of integer;
var
   ListA : ArrayTypeA;
   ListB : ArrayTypeB;
   ListC : ArrayTypeC;

procedure InputData;
begin
     ListA[1]:=1;  ListB[1]:=2;
     ListA[2]:=3;  ListB[2]:=3;
     ListA[3]:=5;  ListB[3]:=4;
     ListA[4]:=6;
end;

procedure Merge(A: ArrayTypeA; B:ArrayTypeB; NA,NB : integer; var C: ArrayTypeC);
var
   pointA,pointB,pointC:integer;
   count:integer;
begin
     pointA:=1;  pointB:=1;  pointC:=1;
     repeat
           if A[pointA]<=B[pointB]
           then begin
                C[pointC]:=A[pointA];
                pointA:=pointA+1
                end
           else begin
                C[pointC]:=B[pointB];
                pointB:=pointB+1
                end;
           pointC:=pointC+1
     until (pointA>NA)or(pointB>NB);
     if pointA>NA
        then for count:=pointB to NB do
                 C[count+NA]:=B[count]
        else for count:=pointA to NA do
                 C[count+NB]:=A[count]
end;

procedure DispalyResults(A: ArrayTypeA; B:ArrayTypeB; NA,NB : integer; var C: ArrayTypeC);
var
   count: integer;
begin
     write('A:');
     for count := 1 to NA do
         write(A[count]:3);
     writeln;
     write('B:');
     for count := 1 to NB do
         write(B[count]:3);
     writeln;
     write('C:');
     for count := 1 to NA+NB do
         write(C[count]:3);
end;

begin
     InputData;
     Merge(ListA,ListB,numA,numB,ListC);
     DIspalyResults(ListA,ListB,numA,numB,ListC);
end.