PROGRAM QuickSort;

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

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

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

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

{---------------------------------------------------------------------Partition}
FUNCTION Partition(VAR Numbers : IntArray; Left, Right : Integer) : Integer;
VAR
    PivotIndex, Pivot : Integer;
BEGIN
    IF Left >= Right THEN
    BEGIN
        Partition := -1;
        Exit;
    END;

    PivotIndex:= Left;
    Pivot := Numbers[PivotIndex];
    Left := Left + 1;

    WHILE True DO
    BEGIN
        WHILE (Left <= Right) AND (Numbers[Left] <= Pivot) DO
            Left := Left + 1;

        WHILE (Left <= Right) AND (Numbers[Right] >= Pivot) DO
            Right := Right - 1;

        IF Left < Right THEN
        BEGIN
            Swap(Numbers, Left, Right);
            Left  := Left + 1;
            Right := Right - 1;
        END
        ELSE
            BREAK;
    END;

    Swap(Numbers, Right, PivotIndex);
    Partition := Right;
END;

{---------------------------------------------------------------------QuickSort}
PROCEDURE QuickSort(VAR Numbers: IntArray; Left, Right : Integer);
VAR
    Pivot : Integer;
BEGIN
    IF Left >= Right THEN
        Exit;

    Pivot := Partition(Numbers, Left, Right);
    QuickSort(Numbers, Left, Pivot-1);
    QuickSort(Numbers, Pivot+1, Right);
END;

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

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

    PrintArray(Numbers);
    QuickSort(Numbers, L, H);
    PrintArray(Numbers);
END.
