title: When I make the line active in the quicksort5 function, it doesn't sort well. But why?
body: <p>Recently I was studying about quicksort,</p>
  
  <p>I wrote 2 programs: One works successfully, while the other doesn't.</p>
  
  <p>I tried to find why the other one is not working.(I know the reason but I want to know the reason under the reason)</p>
  
  <p>The only difference between 2 programs is a line in quicksort5 function,
  which is below:</p>
  
  <pre><code>swap( &amp;list[ ( (backwards-forwards) /2 ) ], &amp;list[last]  );
  </code></pre>
  
  <p>They both includes stdio.h and also have 3 functions which are called main, quicksort5, swap. </p>
  
  <p>The upper lines of the program is below:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int quicksort5(int *, int, int);
  int swap(int *, int *);
  </code></pre>
  
  <p>1) The main function is below:</p>
  
  <pre><code>int main()
  {
      int arrayofintegers[4096];
      int n=0, quantity=0;
  
      printf("Please enter how many integer numbers you want to get sorted: ");
      scanf("%d",&amp;quantity);
  
      if (quantity &lt;= 0)
          return -1;
  
      printf("\nPlease give at max 10 digits per number.\n\n");
      while ( n&lt;quantity ) //import the numbers
      {
          printf("the     %5d. number = ",n+1);
          scanf("%d",&amp;arrayofintegers[n]);
          n++;
      }
      printf("\n");
  
      quicksort5(arrayofintegers, 0, quantity-1);
  
      n=0;
      while ( n&lt;quantity ) //The numbers will be displayed.
      {
          printf("the new %5d. number =%11d\n", n+1, arrayofintegers[n]);
          n++;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>2) The quicksort5 function is below:</p>
  
  <pre><code>int quicksort5(int *list, int forwards, int backwards)
  {
      if ( forwards &gt;= backwards )
          return 0;
  
      int const first   = forwards;
      int const last    = backwards;
  
      /* //If I make the line bellow active the function doesn't sort successfully. But I want to know the main reason in this.
      swap( &amp;list[ ( (backwards-forwards) /2 ) ], &amp;list[last]  ); */
  
      int const pivot = list[last];
  
      int isforwardswaiting  = 0;
      int isbackwardswaiting = 0;
  
  
      backwards--; // the pivot won't change
      while (forwards&lt;backwards)
      {
          isforwardswaiting   = (list[forwards]  &gt;= pivot);
          isbackwardswaiting  = (list[backwards] &lt;  pivot);
  
          if(isforwardswaiting &amp;&amp; isbackwardswaiting)
          {
              swap(&amp;list[forwards],&amp;list[backwards]);
              forwards++;
              backwards--;
          }
          else
          {
              if ( !(isforwardswaiting))
                  forwards++;
  
              if ( !(isbackwardswaiting))
                  backwards--;
          }
      }
  
      if (list[forwards] &lt; pivot)
          forwards++;
  
      swap(&amp;list[forwards],&amp;list[last]); //placing the pivot
  
      /* list[first], list[first+1] ... list[forwards-2], list[forwards-1] ==&gt; the numbers smaller than the pivot
         list[forwards]                                                    ==&gt; the number which is the pivot
         list[forwards+1], list[forwards+2] ... list[last-1], list[last]   ==&gt; the numbers greater than the pivot */
  
      quicksort5(list, first, forwards-1);
      quicksort5(list, forwards+1, last);
  }
  </code></pre>
  
  <p>3) The swap function is below:</p>
  
  <pre><code>int swap(int *a, int *b)
  {
      int c=*a;
      *a=*b;
      *b=c;
      return 0;
  }
  </code></pre>
  
  <p>Thanks in advance for your answers.</p>