
   void QuickSort2(int p, int q)
   // Sorts the elements in a[p:q].
   {
       Stack<int> stack(SIZE); // SIZE is 2*log(n).
        do {
           while (p < q) {
              int j = Partition(a, p, q+1);
              if ((j-p) < (q-j)) {
                 stack.Add(j+1);
                 stack.Add(q); q = j-1;
              }
              else {
                 stack.Add(p);
                 stack.Add(j-1); p = j+1;
              }
           }; // Sort the smaller subfile.
           if (stack.StackEmpty()) return;
           stack.Delete(q); stack.Delete(p);
        } while (1);
   }

