title: Merge sort implementation
body: <p>I am new to c++ and was trying develop a code for merge sort. I tested it with a sample array of size 5 but the answer put out by the code is not right. I can't figure what's going wrong. Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;sstream&gt;
  #include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  void merge(int, int, int, int*);
  void merge_sort(int low, int high, int* p){
      int pivot;
      static int i(1);
      if (high&gt;low)
      {
          cout &lt;&lt; "calling merge_sort: "&lt;&lt;i&lt;&lt;endl; i++;
          pivot = low + ((high - low)/2);
          cout &lt;&lt; pivot &lt;&lt; endl;
          merge_sort(low, pivot, p);
          merge_sort(pivot+1, high, p);
          merge(low, pivot, high, p);
  
      }
  }
  void merge(int l, int pi, int h,int* arr)
  {
              int start = l;
          int mid = pi+1;
          while((start&lt;=pi)&amp;&amp;(mid &lt;=h)){
              if (arr[start] &gt; arr[mid])
              {
                  int temp = arr[mid];
                  arr[mid] = arr[start];
                  arr[start] = temp;
                  mid++;
               }
              else
                  start++;
      }
  }
  int main() 
  {
      int a[] = {2, 42, 3, 7, 1};
      merge_sort(0, 4, a);
      for (int i = 0; i&lt;=4 ; i++)
          cout &lt;&lt; a[i] &lt;&lt; endl;
      return (0);
  
  }
  </code></pre>
  
  <p>The output is as follows:</p>
  
  <pre><code>calling merge_sort: 1
  2
  calling merge_sort: 2
  1
  calling merge_sort: 3
  0
  calling merge_sort: 4
  3
  1
  3
  7
  2
  42
  </code></pre>
  
  <p>I have seen some codes for merge sort implementation on stackoverflow but they use another temporary array, which I want to avoid.</p>
  
  <p>Any help is greatly appreciated in sorting this issue.</p>