title: QuickSort C Median pivot element
body: <p>I'm trying to implement a QuickSort algorithm with a median pivot element...
  My code works, if no numbers occur twice, but this can't be the solution anyway.
  The code works perfect, if i choose the first element of the partition as pivot, no matter if values occur twice or more...</p>
  
  <p>Here is my code:</p>
  
  <pre><code>void quickSort(int a[ ], int from, int to)
  { // sort partition from ... to of array a
      int i, pivot, new_val;
      if (from &lt; to) // at least 2 elements in partition
      {
          //pivot = from; --&gt; old version, first element is pivot
          pivot = median(a, from, to);
          for (i = from; i &lt;= to; i++)
          {
              new_val = a[i];
              if (new_val &lt; a[pivot])
              { // shift values to the right
                  a[i] = a[pivot+1];
                  a[pivot+1] = a[pivot];
                  a[pivot] = new_val;
                  pivot++;
              } // else a[i] stays put
          }
          quickSort(a, from, pivot-1); // left partition
          quickSort(a, pivot+1, to); // right partition
      } // else (from &gt;= to) only 0 or 1 elements
  } // end quicksort
  
  int median(int a[], int from, int to)
  {
      int med = (from + to) / 2;
      if((a[from] &lt; a[med] &amp;&amp; a[med] &lt;= a[to]) || (a[to] &lt; a[med] &amp;&amp; a[med] &lt; a[from]))
          return med;
      else if((a[med] &lt; a[from] &amp;&amp; a[from] &lt; a[to]) || (a[to] &lt; a[from] &amp;&amp; a[from] &lt; a[med]))
          return from;
      else if((a[med] &lt; a[to] &amp;&amp; a[to] &lt; a[from]) || (a[from] &lt; a[to] &amp;&amp; a[to] &lt; a[med]))
          return to;
  }
  </code></pre>
  
  <p>what am i doing wrong??</p>
  
  <p>Thanks in advance!</p>