PROGRAM SelectionSort;

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

PROCEDURE Swap(VAR Numbers : IntArray; I, J : Integer); FORWARD;

{----------------------------------------------------------PROCEDURE PrintArray}
PROCEDURE PrintArray(VAR 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;

{-------------------------------------------------------PROCEDURE SelectionSort}
PROCEDURE SelectionSort(VAR Numbers: IntArray);
VAR
    I, J, MaxIndex : Integer;
BEGIN
    FOR I := High(Numbers) DOWNTO Low(Numbers) DO
    BEGIN
        MaxIndex := I;
        FOR J := Low(Numbers) To I - 1 DO
        BEGIN
            IF Numbers[J] > Numbers[MaxIndex] THEN
                MaxIndex := J;
        END;
        IF MaxIndex <> I THEN
            Swap(Numbers, I, MaxIndex);
    END;
END;

{----------------------------------------------------------------PROCEDURE Swap}
PROCEDURE Swap(VAR Numbers : IntArray; I, J : Integer);
VAR
    Temp : Integer;
BEGIN
    Temp := Numbers[I];
    Numbers[I] := Numbers[J];
    Numbers[J] := Temp;
END;

{---------------------------------------------------------------------MAIN BODY}
VAR
    I       : Integer;
    Numbers : IntArray;
BEGIN
    Randomize;

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

    PrintArray(Numbers);
    SelectionSort(Numbers);
    PrintArray(Numbers);
END.
