title: C quick sort character array into alphabetical order
body: <p>I am fairly new to C and doing this as an exercise. The code compiles and runs before hitting a runtime error Abort trap: 6. I have tracked the bug to the counter <code>j</code> on the second call to the second recursive call meeting <code>i &lt; last_line</code> going out of bounds. I cannot see the error in the code. Quick sort is not a method I am particularly familiar with; can anyone help me find the error?</p>
  
  <p>The list of names I am feeding in is:</p>
  
  <pre><code>James
  Janet
  James
  Rosie
  Dave
  </code></pre>
  
  <p>And here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  void quicksort(char **array, int first_line, int last_line) ;
  
  int main(int argc, char* argv[])
  {
      int i = 0 ;
      int j = 0 ;
      int line_count = 0 ;
      int line_number = 20 ;
      char *lines[20] ;
      //char temp[BUFSIZ] ;
      FILE *foutptr ;
      FILE *finptr ;
  
      finptr = fopen("names.txt", "r") ;
      if(finptr == NULL)
      {
          perror("ERROR: input file pointer") ;
          return 1 ;
      }
  
      foutptr = fopen("sorted.txt", "w") ;
      if(foutptr == NULL)
      {
          perror("ERROR: output file pointer") ;
          return 1 ;
      }
  
      lines[i] = malloc(BUFSIZ) ;
      while(fgets(lines[i],BUFSIZ,finptr) &amp;&amp; i &lt; line_number)
      {
          printf("Element %d is the name %s.\n", i, lines[i]) ;
          i++ ;
          lines[i] = malloc(BUFSIZ) ;
      }
  
      printf("TEST %s.\n", *(lines+1)) ;
      line_count = i ;
      i = 0 ;
      char **all_lines = lines ; 
  
      quicksort(all_lines, 0, line_count-1) ;
  
      for(j = 0 ; j &lt; line_count ; j++)
      {
          fprintf(foutptr, "%s", lines[j]) ;
      }
  
      return 0 ;
  }
  
  void quicksort(char **array, int first_line, int last_line)
  {
      int i = 0, j = 0, x = 0, pivot = (first_line + last_line)/2 ;
      char temp[100] ;
  
      i = first_line ;
      j = last_line ;
  
      if(last_line - first_line &lt; 1)
      {
          return ;
      } 
  
      printf("first_line = %d so i = %d. last line = %d so  j = %d. The pivot is %d \n", first_line, i, last_line, j, pivot) ;
  
      while(i &lt;= j)
      {
          /* While array[i] proceeds the pivot alphabetically and is less than array[j] move through the array. This deals with first mismatching letter of first string is less than the second
     in ASCII character numbers i.e. if earlier in the alphabet as ASCII chartcters earlier in the alphabet have a lower numerical id */    
          while(strcmp(array[i], array[pivot]) &lt; 0 &amp;&amp; i &lt; last_line)
          {
              printf("No swapping In while 1, comparing %s and %s with strcmp = %d. \n", array[i], array[pivot], strcmp(array[i], array[pivot])) ;
              printf("%d. \n", i) ;
              i++ ;
          }
  
          /* While array[j] follows the pivot alphabetically and is greater than array[i] move through the array. This deals with the first mismatching letter of first string compared to the second                    is greater in number id i.e. the ASCII character code is bigger in the first so it is  latter in the alphabet */
          while(strcmp(array[j], array[pivot]) &gt; 0 &amp;&amp; j &gt; first_line)
          {
              printf("No swapping In while 2, comparing %s and %s with strcmp = %d. \n", array[j], array[pivot], strcmp(array[j], array[pivot])) ;
              printf("%d. \n", j) ;
              j-- ;
          }
  
                  // Swap if the conditions above are not met 
          if(i &lt;= j)
          {
              printf("%s\n","SWAPPING") ;
              printf("Array[%d] = %s, Array[%d] = %s", i, array[i], j,  array[j]) ;
              strcpy(temp,array[i]) ;
              strcpy(array[i], array[j]) ; 
              strcpy(array[j],temp) ;
              printf("Array[%d] = %s, Array[%d] = %s", i, array[i], j,  array[j]) ;           
              i++ ;
              j-- ;
          }
      }
  
      if(first_line &lt; j)
      {
          printf("Recursive call 1: Lower bound = %d, Upper bound = %d\n", first_line, j) ;
          for(x = 0 ; x &lt; last_line + 1 ; x++)
          {
              printf("Array[%d]: %s \n", x, array[x]) ;
          }
          quicksort(array, first_line, j) ;
      }
  
      x = 0 ;
  
      if(i &lt; last_line)
      {
          printf("Recursive call 2: Lower bound = %d, Upper bound = %d\n", i, last_line) ;
          for(x = 0 ; x &lt; last_line + 1 ; x++)
          {
              printf("Array[%d]: %s \n", x, array[x]) ;
          }
          quicksort(array, i, last_line) ;
      }
  
      x = 0 ;
      return ;
  }
  </code></pre>