title: Merge sort algorithm in an array in C - .exe not running?
body: <p>The following is my 'C' code for MergeSort algorithm. I'm using Dev C++, using TDM:GCC compiler. Doesn't give compilation error but exe doesnt run after compilation. Please do help.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  
  int main()
  {
      int A[] = {12, 5, 6, 78, 223, 1, 45, 34, 78, 99};
      int n = sizeof(A);
      int i = 0;
  
      A[n] = MergeSort(A[n], n);
  
      for ( i = 0; i &lt; n; i++ ) printf("%d\n", A[i]);
  
      return 0;
  }
  
  int MergeSort(int A[], int n)
  {
      int mid = n/2;
      int left[mid], right[n - mid];
      int i;
  
      if(n&lt;2) return;
  
      for ( i = 0; i &lt; mid; i++ )
          left[i] = A[i];
      for ( i = mid; i &lt; n; i++ )
          right[i] = A[i];
  
      int nL = sizeof(left);
      int nR = sizeof(right);
  
      MergeSort(left, nL);
      MergeSort(right, nR);
      Merge(left, right, nL, nR, mid);
  }
  
  int Merge(int left[], int right[], int nL, int nR, int mid)
  {
      int n = nL + nR;
      int i, j, k = 0;
      int A[n];
  
      for ( k = 0; k &lt; n; k++ )
      {
          if( left[i] &lt;= right[mid + j] )
          {
              A[k] = left[i];
              i = i + 1;
          }
          else
          {
              A[k] = right[mid + j];
              j = j + 1;
          }
  
          if ( i + j &gt;= n) break;
      }
  
      return;
  }
  </code></pre>
  
  <p>My code doesn't run at all, and shows an error window instantaneously.</p>
  
  <p>------ EDIT ------</p>
  
  <p>My new edited code is as follows. No error now, but still the sorting doesn't take place.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  
  int Merge(int left[], int right[], int nL, int nR, int A[])
  {
      int n = nL + nR;
      int i = 0;
      int j = 0;
      int k = 0;
  
      for ( k = 0; k &lt; n; k++ )
      {
          if(i &lt; nL &amp;&amp; j &lt; nR &amp;&amp; left[i] &lt;= right[j] )
          {
              A[k] = left[i];
              i = i + 1;
          }
          else
          {
              A[k] = right[j];
              j = j + 1;
          }
      }
  
      return;
  }
  
  void MergeSort(int A[], int n)
  {
      int mid = n/2;
      int left[mid], right[n - mid];
      int i;
  
      if(n&lt;=1) return;
  
      for ( i = 0; i &lt; mid; i++ )
          left[i] = A[i];
      for ( i = 0; i &lt; n - mid; i++ )
          right[i] = A[i + mid];
  
      int nL = sizeof(left)/sizeof(left[0]);
      int nR = sizeof(right)/sizeof(right[0]);
  
      MergeSort(left, nL);
      MergeSort(right, nR);
      Merge(left, right, nL, nR, A);
  }
  
  int main()
  {
      int A[] = {98, 5, 6, 78, 223, 1, 45, 34, 78, 99};
      int n = sizeof(A)/sizeof(A[0]);
      int i = 0;
  
      MergeSort(A, n);
  
      for ( i = 0; i &lt; n; i++ ) printf( "%d\n", A[i] );
  
      return 0;
  }
  </code></pre>
  
  <p>--- LATEST EDIT ---</p>
  
  <p>The new code, with modifications in Merge, works as follows:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  
  int Merge(int left[], int right[], int nL, int nR, int A[])
  {
      int n = nL + nR;
      int i = 0;
      int j = 0;
      int k = 0;
  
      for ( k = 0; k &lt; n; k++ )
      {
          if(j &gt;= nR || (i &lt; nL &amp;&amp; left[i] &lt;= right[j]))
          {
              A[k] = left[i];
              i = i + 1;
          }
          else
          {
              A[k] = right[j];
              j = j + 1;
          }
      }
  
      return;
  }
  
  void MergeSort(int A[], int n)
  {
      int mid = n/2;
      int left[mid], right[n - mid];
      int i;
  
      if(n&lt;=1) return;
  
      for ( i = 0; i &lt; mid; i++ )
          left[i] = A[i];
      for ( i = 0; i &lt; n - mid; i++ )
          right[i] = A[i + mid];
  
      int nL = sizeof(left)/sizeof(left[0]);
      int nR = sizeof(right)/sizeof(right[0]);
  
      MergeSort(left, nL);
      MergeSort(right, nR);
      Merge(left, right, nL, nR, A);
  }
  
  int main()
  {
      int A[] = {98, 5, 6, 78, 223, 1, 45, 34, 78, 99};
      int n = sizeof(A)/sizeof(A[0]);
      int i = 0;
  
      MergeSort(A, n);
  
      for ( i = 0; i &lt; n; i++ ) printf( "%d\n", A[i] );
  
      return 0;
  }
  </code></pre>
  
  <p>Thanks to chqrlie and others for all the effort! :)</p>