title: Merge Sort Error : Segmentation Fault
body: <p>I'm getting an error <code>Segmentation Fault:11</code>, please help.
  variable info:(s:start, e:end, m:mid, n:array), testing for a sample array <code>n[] = {4,3,2,1}</code>. <code>a1</code> and <code>a2</code> are temporary arrays. I am guessing there's something with the calculation of <code>m:mid</code> and passing it.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  void merge(int s, int e, int m, int n[]) {
      int l1 = m - s;
      int l2 = e - m + 1;
      int a1[l1];
      int a2[l2];
      for (int i = 0; i &lt; l1; i++) {
          a1[i] = n[s + i];
      }
      for (int j = 0; j &lt; l2; j++) {
          a2[j] = n[s + m + j];
      }
  
      int i = 0, j = 0; 
      for (int k = 0; k &lt; l1 + l2; k++) {
          if (a1[i] &lt;= a2[j] &amp;&amp; i != l1 &amp;&amp; j != l2) {
              n[k] = a1[i];
              i++;
          } else if (a2[j] &lt;= a1[i] &amp;&amp; i != l1 &amp;&amp; j != l2) {
              n[k] = a2[j];
              j++;
          } else if (j == l2 &amp;&amp; i != l1) {
              n[k] = a1[i];
              i++;
          } else if(i == l1 &amp;&amp; j != l2) {
              n[k] = a2[j];
              j++;
          }
      }
  }
  
  void mergeSort(int s, int e, int n[]) {
      if (s &lt; e) {
          int m = (e - s) / 2;
          mergeSort(s, m - 1, n);
          mergeSort(m, e, n);
          merge(s, e, m, n);
      }
  }
  
  int main(void) {
      int n[] = { 4, 3, 2, 1 };
      int r = 4;
      mergeSort(0, r - 1, n);
  
      for(int i = 0; i &lt; r; i++) {
          printf("%i\n", n[i]);
      }
  }
  </code></pre>