title: Non recursive QuickSort
body: <p>I'm curious to know has my implementation of non recursive QuickSort algorithm some drawbacks or hidden rocks. What should be modified in order to optimize it? And what problems could happen when comparing two  objects in the way I do it?</p>
  
  <pre><code>public class QuickSort &lt;T extends Number&gt; {
  
  private Integer first, last, boundLo, boundHi, pivot;
  Integer temp[] = {0, 0};
  
  public void sort(NewArrayList&lt;T&gt; vect) {
      Deque&lt;Integer[]&gt; stack = new ArrayDeque&lt;Integer[]&gt;();
  
      first = 0;
      last = vect.size() - 1;
      stack.push(new Integer[] {first, last});
  
      while(!stack.isEmpty()) {
          sortStep(vect, stack);  
      }
  }
  
  private void sortStep(NewArrayList&lt;T&gt; vect, Deque&lt;Integer[]&gt; stack) {
      // initialize indices
      temp = stack.pop();
      first = temp[0];
      last = temp[1];
  
      boundLo = first;
      boundHi = last;
      pivot = last;
  
      while(first &lt; last) {
          if(vect.get(first).doubleValue() &gt;= vect.get(pivot).doubleValue()) {
              last--;
              if(first != last) 
                  vect.swap(first, last);         
              vect.swap(last, pivot);
              pivot--;
          }
          else first++;
      }
  
      if(boundLo &lt; (pivot - 1)) 
          stack.add(new Integer[] {boundLo, pivot - 1});
  
      if(boundHi &gt; (pivot + 1)) 
          stack.add(new Integer[] {pivot + 1, boundHi});
  
  }
  
  }
  </code></pre>
  
  <p>And is ArrayList the best collection for this kind of sort?</p>
  
  <pre><code>public class  NewArrayList&lt;T&gt; extends ArrayList&lt;T&gt; {
  
  public NewArrayList() {
      super();
  }
  
  public void swap(int index1, int index2) {
      this.set(index1, this.set(index2, this.get(index1)));
  } 
  }
  </code></pre>
  
  <p>The code after modifying in consideration of the suggestions </p>
  
  <pre><code>public class QuickSort &lt;T extends Number&gt; {
  
  private int first, last, boundLo, boundHi, pivot;
  int temp[] = {0, 0};
  
  public QuickSort() {
      super();
  }
  
  public void sort(List&lt;T&gt; list) {
  
      Deque&lt;int[]&gt; stack = new ArrayDeque&lt;int[]&gt;();
  
      first = 0;
      last = list.size() - 1;
  
      stack.push(new int[] {first, last});
  
      while(!stack.isEmpty()) {
          sortStep(list, stack);  
      }
  }
  
  private void sortStep(List&lt;T&gt; list, Deque&lt;int[]&gt; stack) {
  
      temp = stack.pop();
      first = temp[0];
      last = temp[1];
  
      boundLo = first;
      boundHi = last;
      pivot = last;
  
      while(first &lt; last) {
          if(list.get(first).doubleValue() &gt;= list.get(pivot).doubleValue()) {
              last--;
              if(first != last) 
                  Collections.swap(list, first, last);            
              Collections.swap(list, last, pivot);
              pivot--;
          }
          else first++;
      }
  
      if(boundLo &lt; (pivot - 1)) 
          stack.add(new int[] {boundLo, pivot - 1});
  
      if(boundHi &gt; (pivot + 1)) 
          stack.add(new int[] {pivot + 1, boundHi});
  }
  
  }
  </code></pre>