title: Iterative Quicksort
body: <p>I'm trying to implement a quicksort method that runs iterativly. I have used stack to hold the information. It will also use partitions to achieve this. I know that the partitioned section of code at the bottom is fine, it's only the first block of it that's having issues. For some reason though, my code is not doing what its suppose to. Not too experienced with java, so if anyone sees any errors that would throw a flag it would be much appreciated! </p>
  
  <pre><code>import java.util.Stack;
  
  public class QuickSort{
  
  // provide non-recursive version of quick sort
  // hint: use stack to stored intermediate results
  // java.util.Stack can be used as stack implementation
  public static &lt;T extends Comparable&lt;T&gt;&gt; void sort(T[] a) {
      Stack&lt;Integer&gt; stack = new Stack&lt;Integer&gt;();
  
        stack.push(0);
        stack.push(a.length);
  
        while (!stack.isEmpty())
        {
           int i = 0; 
           int hi = a[i]
           hi = stack.pop();
           int lo = stack.pop();
  
           if (hi - lo &lt; 2) {
               continue;
               }
  
  
           int j = partition(a, lo, hi);
           j = hi + ((lo - hi) / 2);
  
           stack.push(j - 1);
           stack.push(hi);
           stack.push(lo);
           stack.push(j);    
  
        } 
           // return;
  }
  
  //THIS SECTION OF CODE BELOW SHOULD BE FINE 
  
  // Partition into a[lo..j-1], a[j], a[j+1..hi]
  private static &lt;T extends Comparable&lt;T&gt;&gt; int partition(T[] a, int lo, int hi) { 
      int i = lo, j = hi + 1; // left and right scan indices
      T v = a[lo]; // the pivot
  
      while (true) { // Scan right, scan left, check for scan complete, and exchange
          while (SortUtils.isLessThan(a[++i], v)) {//++i is evaluated to i+1 
              if (i == hi) {
                  break;
              }
          }
          while (SortUtils.isLessThan(v, a[--j])) {//--j is evaluated to j-1
              if (j == lo) {
                  break;
              }
          }
          if (i &gt;= j) {
              break;
          }
  
          SortUtils.swap(a, i, j);
      }
  
      SortUtils.swap(a, lo, j); // Put v = a[j] into position
      return j; 
  }
  
  }
  </code></pre>
  
  <p>TEST CODE</p>
  
  <pre><code>package edu.csus.csc130.spring2017.assignment2;
  
  import java.util.Arrays;
  
  import org.junit.Assert;
  import org.junit.Test;
  
  public class 
  
  QuickSortTest {
  
  @Test
  public void testSort1() {
      Integer[] a = {17};
      Integer[] expected = {17};
      QuickSort.sort(a);
      System.out.println(Arrays.toString(a));
      Assert.assertArrayEquals(expected, a);
  }
  
  @Test
  public void testSort2() {
      Integer[] a = {17, 5};
      Integer[] expected = {5, 17};
      QuickSort.sort(a);
      System.out.println(Arrays.toString(a));
      Assert.assertArrayEquals(expected, a);
  }
  
  @Test
  public void testSort3() {
      Integer[] a = {64, 18, 74, 89, 58, 17, 48, 44, 92, 88, 78, 80, 75, 25, 77, 18, 39, 95, 11, 2};
      Integer[] expected = {2, 11, 17, 18, 18, 25, 39, 44, 48, 58, 64, 74, 75, 77, 78, 80, 88, 89, 92, 95};
      QuickSort.sort(a);
      System.out.println(Arrays.toString(a));
      Assert.assertArrayEquals(expected, a);
  }
  
  
  }
  </code></pre>