#implementation of mergesort due to eric youngblut

# I stole this implementation of mergesort from the current 143 textbook,
# because I didn't have any other braindead, no-frills implementation hanging
# around.


module main;

const N: int = 10;

procedure Mergesort(var A: array [N] of int, F: int, L: int);

    procedure Merge(F: int, Mid: int, L: int);
        var TempArray: array [N] of int;
        var First1: int;
        var Last1: int;
        var First2: int;
        var Last2: int;
        var Index: int;

    begin
        First1 := F;
        Last1  := Mid;
        First2 := Mid + 1;
        Last2  := L;
        Index  := First1;

        while First1 <= Last1 and First2 <= Last2 do
            if A[First1] < A[First2] then
                TempArray[Index] := A[First1];
                First1 := First1 + 1;
            else
                TempArray[Index] := A[First2];
                First2 := First2 + 1;
            end;
            Index := Index + 1;
        end;

        while First1 <= Last1 do
            TempArray[Index] := A[First1];
            First1 := First1 + 1;
            Index  := Index  + 1;
        end;

        while First2 <= Last2 do
            TempArray[Index] := A[First2];
            First2 := First2 + 1;
            Index  := Index  + 1;
        end;

        for Index := F to L do
            A[Index] := TempArray[Index];
        end;
    end Merge; 

    var Mid: int;

begin
    if F < L then
        Mid := (F + L) / 2;
        Mergesort(A, F, Mid);
        Mergesort(A, Mid+1, L);
        Merge(F, Mid, L);
    end;
end Mergesort;



var data: array [N] of int;
var i: int;

begin
    data[0] := 102;
    data[1] := 103;
    data[2] := 107;
    data[3] := 105;
    data[4] := 104;
    data[5] := 109;
    data[6] := 108;
    data[7] := 101;
    data[8] := 106;
    data[9] := 100;

    Mergesort(data, 0, 9);

    for i := 0 to N - 1 do
        output := data[i];
    end;

end main.