program QuickSort{

var
{
    Integer[] A; 
}

    function qSort(Integer low, Integer high):void;
    var
    {
        Integer i, j, x, temp;
    }
    {
        i = low;
        j = high;
        x = A[(low+high)/2];
        do 
        {
            while(A[i] < x) i = i + 1;
            while(A[j] > x) j = j - 1;
            if(i <= j){
                temp = A[i];
                A[i] = A[j];
                A[j] = temp;
                i = i + 1; 
                j = j - 1;
            }
        } 
        while(i <= j);
        if(low < j) qSort(low, j);
        if(i < high) qSort(i, high);
    }

    function printArray():void;
    var
    {
        Integer i,len; 
    }
    {
        len = length(a);
        for(i = 0;i<len;i = i + 1)
        {
            write("a[" + i + "] = " + a[i] + (i == len - 1 ? "" : ", "));
        }
        writeLn("");
    }

    function main():void;
    {
        a = new Integer[]{1, 5, 8, 3, 6, 2, 9};
        printArray();
        qSort(0,length(a)-1);
        printArray();
    }
}