title: Different C compilers giving different results for Merge Sort
body: <p>I am running a merge sort implementation and it is giving correct results in <a href="https://code.hackerearth.com/4a1c9cn" rel="nofollow noreferrer">https://code.hackerearth.com/4a1c9cn</a> but wrong results in <a href="https://www.tutorialspoint.com/compile_c_online.php" rel="nofollow noreferrer">https://www.tutorialspoint.com/compile_c_online.php</a>.</p>
  
  <p>Can somebody tell me the mistakes and the things to keep in mind to avoid such mistakes in the future.
  Sample test case: 5 3 7 5 9 2
  Wrong answer by the second compiler: 03579
  The code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void merge(int A[], int left[], int right[], int n)
  {
       int i=0, j=0, k=0, m = n/2, d = n-m;
       while(i&lt;m &amp;&amp; j&lt;d)
       {
           if(left[i] &lt;= right[j]) A[k++] = left[i++];
           else A[k++] = right[j++];
       }
      if(i == m)
          for(;k&lt;n; k++, j++) A[k] = right[j];
      else if(j == d)
          for(; k&lt;n; k++, i++) A[k] = left[i];
      return;
  }
  
  void MergeSort(int A[], int n)
  {
      if(n==1) return;
      int m = n/2;
      int* left = (int*)malloc(m*sizeof(int));
      int* right = (int*)malloc((n-m)*sizeof(int));
      int i, j;
      for(i=0; i&lt;m; i++) left[i] = A[i];
      for(i=m; i&lt;n; i++, j++) right[j] = A[i];
      MergeSort(left, m);
      MergeSort(right, (n-m));
      merge(A, left, right, n);
      return;
  } 
  
  int PrintArr(int A[], int n);
  void getArr (int A[], int n);
  
  int main(int argc, char** argv)
  {
      int n, *arr;
      scanf("%d", &amp;n);
      arr = (int*)malloc(n*sizeof(int));
      getArr(arr, n);
      MergeSort(arr, n);
      PrintArr(arr, n);
      return 0;
  }
  </code></pre>