---
# System prepended metadata

title: 'Merge2SortedLists(P.439)[LO]'
tags: [Pascal Programming D1]

---

###### tags: `Pascal Programming D1`
#    Merge2SortedLists(P.439)[LO]
#    Not surprised if there're typos X)

program Merge2;
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 + NB] := B[count]
      else for count := pointA to NA do
           C[count + NA] := A[count]
end;

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

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