title: Why this QuickSort Partition method is wrong?
body: <p>I have a quickSort Partition method.
  But I don't see why it is wrong</p>
  
  <p>the method is here:</p>
  
  <pre><code>private static &lt;E extends Comparable&lt;E&gt;&gt; int partition(E[] list, int first, int last) {
      int pivotIndex = (first + last) / 2;
      E pivot = list[pivotIndex]; // Choose the first element as the pivot
      swap(list, last, pivotIndex);
      pivotIndex = last;
      last--;
      do {
          // Search forward from left
          while (first &lt; last &amp;&amp; list[first].compareTo(pivot) &lt;= 0)
              first++;
          // Search backward from right
          while (first &lt;= last &amp;&amp; list[last].compareTo(pivot) &gt; 0)
              last--;
  
          // Swap two elements in the list
          if (last &gt;= first) {
              swap(list, first, last);
              first++;
              last--;
          }
      } while (last &gt; first);
  
      swap(list, pivotIndex, first);
  
      return first;
  }
  </code></pre>
  
  <p>This is how it is called using recursive:</p>
  
  <pre><code>quickSort ( array )
  quickSortRec( array, 0, array.size - 1 )
  quickSortRec (array, left, right)
  pivotIndex = findpivot(array, left, right) //use any method
  newPivotIndex = partition ( array, left, right, pivotIndex )
  if ( newPivotIndex - left &gt; 1 )
  quickSortRec( array, left, newPivotIndex - 1 )
  if ( right - newPivotIndex &gt; 1 )
  quickSortRec( array, newPivotIndex + 1, right )
  </code></pre>
  
  <p>I know the bugs lie in the do while loop but I don't know why and how. I don't need the correct version of the partition method... I just want to know why this one is wrong. For example, if I want to sort [12  28  79  19  60  22  3  50  75  60  25  97  98  12  88 ] it gives me [3  12  19  22  25  12  28  50  60  60  75  79  88  97  98] which is wrong...</p>