title: How to implement Quicksort in C with pointers?
body: <p>So I am currently translating my Java quicksort program into C, but I am unsure if I am using pointers correctly. As far as I know I believe I am using them correctly in my partition method or at least my syntax is correct -- I am not getting any errors. I am; however, getting the same array as my result. My current program is </p>
  
  <pre><code>' 
  // quickSort.c
        #include &lt;stdio.h&gt;
        #include &lt;stdlib.h&gt;
        #
        void swap(int *a, int *b);
        void quickSort( int[], int, int);
        int partition( int*, int, int);
        int main(int argc, char *argv[])
        {
          int a[] = {2, 6, 5, 1}; 
          int i;
         // int arrlength = sizeof(a); WRONG
          int arrlength = 4; 
          printf("\n\nUnsorted array is:  ");
          for(i = 0; i &lt; arrlength; ++i)
              printf(" %d ", a[i]);
  
          //array, 0, length 
          quickSort( a, 0, arrlength);
  
          printf("\n\nSorted array is:  ");
          for(i = 0; i &lt; arrlength; ++i)
              printf(" %d ", a[i]);
          printf("\n\n");
  
        }
  
  
        // char yo[] = "Hello"; 
        // &amp;yo[0] = will be equivalent to = Hello &amp;yo[0] points to variable yo[0]
  
        //array, Leftmost position, Rightmost position
        void quickSort( int a[], int L, int R)
        {
           int k;
  
           if( R &lt;= L){
            return;
           }
            k = partition( a, L, R);
            quickSort( a, L, k); // Sort left half of partitioned array
            quickSort( a, k+1, R);  // Sort right half of partitioned array
        }
  
  
  
  
        // L= leftmost position in the array 
        // R= rightmost position in the array 
        // int *a = pointer to array we are sorting 
  
        int partition(int a[], int L, int R) {
  
         // [2   , 6 , 5 , 1] 
         //  |             |
         //  |             |   once the two pointers cross we exist the while loop. 
         //  |             |
         //  S             B=4 &amp; p[B]=1
  
        int pivot; 
        int help; 
        int S; 
        int B; 
        S = L;  //S = current Leftmost position
        B = R-1;//B = rightmost position
        pivot = a[R-1];     //rightmost number in array
        int *pop; 
        pop = &amp;a[B]; //pop points to address of array 
        //*pop is the actual variable in the address a[B]
  
        while(B &gt; S) {//as long as the two pointers don't cross you continue
          if(a[B-1] &gt; pivot){   //move to the right of the pivot 
            pop = &amp;a[B-1]; 
            B--; 
          }
          else {
            swap(&amp;a[B-1], &amp;a[S]);// move to left of the pivot 
            S++; 
          }
        }
  
          pop = &amp;pivot;  // Put pivot in the "hole" 
          return B;  //return the position of the pivot
  
        }
  
        void swap(int *a, int *b){
          int temp; 
          temp = *a; 
          *a = *b; 
          *b = temp; 
        }
  
  
        // Unsorted array is:   2  6  5  1 
  
        // Sorted array is:   2  6  5  1 
  
        // First partition: 
        // pivot = 1; 
        // 2,6,5,5
        // 2,6,6,5
        // 2,2,6,5
        // escape while loop
        // 1,2,6,5
  
   '
  </code></pre>
  
  <p>In case anyone wants to see my Java program, this is it: 
  '
          import java.util.Arrays;</p>
  
  <pre><code>    public class AltSorts {
  
       public static void QuickSort( double[] a, int L, int R ){
           int k;
           if ( R &lt;= L){ 
               return;
              }   
           k = partition( a, L, R );
  
          QuickSort( a, L, k );   // Sort left half of partitioned array
          QuickSort( a, k+1, R); // Sort right half of partitioned array
  
      }
  
       public static int partition( double[] a, int L, int R )
       {
          double   pivot;
          double   help;
          int      S;
          int     B;
      /**
       [2     ,   6   ,   5   ,   1] 
        |                         |
        |                         |       once the two pointers cross we exist the while loop. 
        |                         |
        S                         B=4 &amp; p[B]=1
       * */
          S = L;  //S = current Leftmost position
          B   = R-1;//B = rightmost position
          pivot = a[R-1];     //rightmost number in array
  
          while ( B &gt; S )//as long as the two pointers don't cross you continue
          {
               if ( a[B -1] &gt; pivot ){          //move to the right of the pivot 
                a[B] = a[B -1];
                B--;
           }
             else
  
             {  
                 help = a[B -1]; // move to left of the pivot 
                 a[B -1] = a[S];
                 a[S] = help;
                 S++;
             }
          }
          a[B] = pivot;   // Put pivot in the "hole" 
          return B; //return the position of the pivot
       }
       public static void main( String[] args )
       {
           double[] x = {2, 6, 5, 1}; 
          System.out.println("Before sort:    " + Arrays.toString(x) + "\n" );  
  
          QuickSort( x, 0, x.length );     // Quick sort
  
          System.out.println("\nAfter sort:     " + Arrays.toString(x) );
       }
      }
  </code></pre>
  
  <p>'</p>