title: Merge Sort in C compiles, but doesn't sort
body: <p>I made this program to sort an array. It works fine, but it won't sort! Please help me find the error in my logic. Thanks</p>
  
  <p>[UPDATE] It was able to work! I just brought down the i, j, and k as suggested below.Also, from i
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void mergesort(int[], int, int);
  void merge(int [], int low, int mid, int hi); //function prototype
  
  int main()
  {
      int arr[]={1,4,78,92,9};
      mergesort(arr,0,5);
      //after mergesort
      for(int i=0; i&lt;5; i++)
      {
          printf("%d, ", arr[i]);
      }
      system("pause");
      return 0;
  }
  
  void mergesort(int aptr[], int low, int hi)
  {
      int mid =0;
      int rightmax=0;
      int leftmax=0;
  
      if(low==hi)
      {
          return;
      }
      mid=(low+hi)/2;
      mergesort(aptr, low, mid);
      mergesort(aptr, mid+1, hi);
      merge(aptr, low, mid, hi);
  }
  
  void merge(int aptr[], int low, int mid, int hi)
  {
      int j, i, k;
  
      //copy contents of aptr to auxiliary b 
      for(i=low; i&lt;=hi; i++)
      {
          bptr[i]=aptr[i]; 
      }
  
      // iterate through b as if they were still two arrays, lower and higher
      //copy smaller elements first
      i=low;
      j=mid+1;
      k=low;
  
      while(i&lt;= mid &amp;&amp; j&lt;=hi)
      {
          if(bptr[i]&lt;=bptr[j])//&lt;--put smaller element first
          {
              aptr[k++]=bptr[i++];
          }
          else
          {
              aptr[k++]=bptr[j++];
          }
      } 
      // copy back first half just in case
      while(i&lt;=mid)
      {
          aptr[k++]=bptr[i++];
      }
     }//function
  </code></pre>