PROGRAM BubbleSort;

TYPE
    IntArray = ARRAY[1..10] OF Integer;

{--------------------------------------------------------------------BubbleSort}
Procedure BubbleSort(VAR Numbers : IntArray);
VAR
    I, J, Temp : Integer;
    Swapped    : Boolean;
BEGIN
    FOR I := High(Numbers) DOWNTO Low(Numbers) DO
    BEGIN
        Swapped := False;
        FOR J := Low(Numbers) TO I - 1 DO
            IF Numbers[J] > Numbers[J+1] THEN
            BEGIN                               
                Temp := Numbers[J+1];
                Numbers[J+1] := Numbers[J];
                Numbers[J] := Temp;
                Swapped := True;
            END;

        IF NOT Swapped THEN
            Exit;
    END;
END;

{--------------------------------------------------------------------PrintArray}
PROCEDURE PrintArray(CONST Numbers : IntArray);
VAR
    I, H : Integer;
BEGIN
    H := High(Numbers);

    FOR I := Low(Numbers) TO H DO
    BEGIN
        Write(Numbers[I]);
        IF I = H THEN
            WriteLn
        ELSE
            Write(' ');
    END;
END;

{---------------------------------------------------------------------main body}
VAR
    I       : Integer;
    Numbers : IntArray;
BEGIN
    Randomize;

    FOR I := Low(Numbers) TO High(Numbers) DO
        Numbers[I] := Random(100);

    PrintArray(Numbers);
    BubbleSort(Numbers);
    PrintArray(Numbers);
END.
