Can anyone tell me why this search parition for quicksort works? private static &lt; T extends Comparable &lt; ?super T &gt;&gt; int partition(T[] a, int first, int last) {
  int mid = (first + last) / 2;
  sortFirstMiddleLast(a, first, mid, last);

  swap(a, mid, last - 1);
  int pivotIndex = last - 1;
  T pivot = a[pivotIndex];
 
  int indexFromLeft = first + 1;
  int indexFromRight = last - 2;
  boolean done = false;
  while (!done) {
   
    while (a[indexFromLeft].compareTo(pivot) &lt; 0)
    indexFromLeft++;

    while (a[indexFromRight].compareTo(pivot) &gt; 0)
    indexFromRight--;
  
    if (indexFromLeft &lt; indexFromRight) {
      swap(a, indexFromLeft, indexFromRight);
      indexFromLeft++;
      indexFromRight--;
    }
    else done = true;
  } 
  swap(a, pivotIndex, indexFromLeft);
  pivotIndex = indexFromLeft;
 
  return pivotIndex;
} 

It doesn't seem to touch the first or last elements
sortFirstMiddleLast() sorts the first, middle, and last elements in-place. Once that's done, there's no need to consider the first or last value because (if the middle value is used as the pivot) they're known to be correctly partitioned.