title: Quicksort Depends on Choosing Pivot
body: <p>This is my quicksort algorithm, including partition and swap. Works well, when I'm choosing as a pivot the last element of the array (in function quicksort: <code>int r = partition(a, n, n);</code>), but fails when taking the first: <code>int r = partition(a, n, s);</code>     </p>
  
  <pre><code>void Swap(int a[], int l, int r){
  int tmp = a[l];
  a[l] = a[r];
  a[r] = tmp;
  }
  
  int partition(int a[], int n, int p) {
  Swap(a, p, n);
  int l = 0;
  for (int i = 1; i &lt;= n - 1; i++) {
      if (a[i] &lt;= a[n]) {
          l += 1;
          Swap(a, l, i);
          }
      }
  Swap(a, p, l + 1);
  return l + 1;
  }
  
  void quicksort(int a[], int s, int n) {
  if (s &lt; n) {
      int r = partition(a, n, n);
      quicksort(a, s, r - 1);
      quicksort(a, r + 1, n);
      }
  }
  </code></pre>