title: Quicksort returning unsorted middle/median element?
body: <p>I am fairly new to programming and have been learning sorting algorithms for a while now.I have implemented Quick sort below in C. It uses median of three partitioning to select the pivot. 
  The problem with the code is every time I run it, the element that is supposed to be the median/ mid value of the sorted array is unsorted, ie it occurs somewhere else.
  Here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;limits.h&gt;
  
  void swap(int *x, int *y)
  {
      int temp = *x;
      *x = *y;
      *y = temp;
      return ;
  }
  
  int median3(int a[], int left, int right)//Uses median of three partitioning technique
  {
      int center = (left + right)/2;
      if (a[center] &lt; a[left]) 
          swap(&amp;a[left],&amp;a[center]);
      if (a[right] &lt; a[left])
          swap(&amp;a[left],&amp;a[right]);
      if (a[right]&lt; a[center])
          swap(&amp;a[center],&amp;a[right]);
  
      swap(&amp;a[center], &amp;a[right - 1]);//since the largest is already in the right.
      return a[right - 1];
  }
  
  void quicksort(int a[], int left, int right)
  {
    if (left &lt; right) {
      int pivot = median3(a,left,right);
      int i = left;
      int j = right - 1;
      for ( ; ;) {
          while(a[++i]&lt;pivot) {} 
          while(pivot&lt;a[--j]) {} 
          if ( i &lt; j)
              swap(&amp;a[i],&amp;a[j]);
          else
              break ;
      }
      swap(&amp;a[i],&amp; a[right -1]);
      quicksort(a,left,i-1);
      quicksort(a,i+1,right);
    }
      return ;
  }
  
  int main(int argc, char* argv[])
  {
      int i;
      int a[] = {8,1,4,9,6,3,5,2,7,0};
      quicksort(a,0,9);
      for ( i = 0 ; i &lt; 10 ; i++)
          printf("%d\n",a[i]);
      return 0;
  }
  </code></pre>