[Solved] c++ merge sort trouble [closed]


So one small mistake that you don’t take into account is that, you are passing pointer array ini but inside the method you still use the method initial and one more thing is, you should take in a temp array that would assign the sorted values to your ini array.

So your code should look something like this

  void sort(int* ini, int left, int right, int m){
     int first_h = left;
     int second_h = m+1;
     int out = left;
     while(first_h <= m && second_h <= right){
          if(ini[first_h] < ini[second_h]){
               fin[out] = ini[first_h];
              first_h++;
          }
          else{
               fin[out] = ini[second_h];
               second_h++;

          }  
          out++;   
     }

     while(first_h <= m){
          fin[out] = ini[first_h];
          out++;
      first_h++;
     }
     while(second_h <= right){
          fin[out] = ini[second_h];
          out++;
          second_h++;
     }
     for(int i=left; i<out; i++)
     {
       ini[i] = fin[i];
     }
}

Hope this helps in understanding the algorithm better!

7

solved c++ merge sort trouble [closed]