title: Generic QuickSort causing StackOverflowError
body: <p>This is a practice problem in the Java book I'm working through. Basically, the goal is to sort an array of generic-type elements in increasing order using compareTo. </p>
  
  <p>I'm trying to use QuickSort to do it. Here's my code:</p>
  
  <pre><code>public static &lt;T extends Comparable&lt;? super T&gt;&gt;
  void sort (T[] arr)
  {
      // If arr is null, empty,
      // or only has 1 element,
      // it is already sorted
  
      if (arr == null || arr.length == 0
              || arr.length == 1)
          return;
  
      // Call overloaded sort method
  
      sort(arr, 0, arr.length - 1);
  }
  
  // HELPER METHOD FOR SORT METHOD
  
  public static &lt;T extends Comparable&lt;? super T&gt;&gt;
  void sort(T[] arr, int left, int right)
  {
  
      // To be used while sorting
  
      int i = left;
      int j = right;
  
      // Check if left and
      // right indices are out
      // of order. If they are,
      // nothing can be done
  
      if (right &lt;= left)
          return;
  
      // Middle element used
      // as pivot
  
      T pivot = arr[(left + (right - left)) / 2];
  
      // temp will be used
      // to swap elements later
  
      T temp;
  
      // QuickSort algorithm
  
      while (i &lt;= j)
      {
          // Look for values on
          // the left greater than
          // the pivot and values
          // on the right smaller
          // than the pivot
  
          // When you find both, swap them
  
          while (arr[i].compareTo(pivot) &lt; 0)
          {
              i++;
          }
  
          while (arr[j].compareTo(pivot) &gt; 0)
          {
              j--;
          }
  
          // Check that i hasn't
          // passed j already
  
          if (i &lt;= j)
          {
  
              // Swap the items
  
              temp = arr[i];
              arr[i] = arr[j];
              arr[j] = temp;
  
              // Move both indices
              // to their next position
  
              i++;
              j--;
          }
      }
  
      // Recursive calls to the
      // sort method
  
      if (left &lt; j)
          sort(arr, left, j);
      if (i &lt; right)
          sort(arr, i, right);
  }
  </code></pre>
  
  <p>The trouble is, when I test this using the following array:</p>
  
  <pre><code>String[] wordArray = {"Droplet", "Blueberry", "Elephant",
              "Fate", "Apple", "Coconut", "Darjeeling"};
  </code></pre>
  
  <p>I get a StackOverflowError at the following line:</p>
  
  <pre><code>while (arr[i].compareTo(pivot) &lt; 0)
  </code></pre>
  
  <p>And then a bunch of repeating ones at this line:</p>
  
  <pre><code>sort(arr, i, right);
  </code></pre>
  
  <p>The repeating errors at the line above tells me it might have something to do with infinite recursion happening, but I don't know why it would be.</p>
  
  <p>I also don't know why it's throwing the error at the while loop line... it looks like the logic I used in comparing arr[i] to the pivot is okay?</p>