0

This is my code,I've run an iterator i in the first array and swapped each element with the min_element in array 2 and then called sort on the second array.But the output isn't sorted at all.I dont know whats wrong with this approach.

#include<bits/stdc++.h>

class Solution{
public:

void swap(int *a,int *b){
    int temp=*a;
    *a=*b;
    *b=temp;
    return;
}

void merge(int arr1[], int arr2[], int n, int m) 
{
    int i,j=0;
    while(i<n){
       if(arr1[i]<arr2[j])
       {
           i++;
       }
       else{
           swap(arr1+i,min_element(arr2,arr2+m));
           i++;
       }
    }
    sort(arr2,arr2+m);
        
}
};

For Input:

4 5 //size
1 3 5 7 //arr1
0 2 6 8 9 //arr2

your output is: 
1 3 5 7 0 2 6 8 9
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 2
    Please post a [mcve]. – molbdnilo Oct 14 '20 at 08:12
  • 1
    See also: [Why should I not #include ?](https://stackoverflow.com/q/31816095/3422102) – David C. Rankin Oct 14 '20 at 08:14
  • Just fyi, in-place merge sort is far more complicated than you probably realize, but I suspect you're starting to understand that. One of the many reasons `std::inplace_merge` is so revered. Regardless, a proper [mcve] is missing from this post. Provide one by updating your question. – WhozCraig Oct 14 '20 at 08:21
  • 2
    this looks like leetcode, which puts you in some special environment where you only implement methods of a `Solution` class and the rest is a black box. If you want to do any serious debugging you need to get out of there. Write your own main, call your methods, write your own tests, use your own debugger – 463035818_is_not_an_ai Oct 14 '20 at 08:24
  • `int i,j=0;` does not actually initialize `i` – Gianni Oct 14 '20 at 13:36
  • An in place stable merge normally involves rotating portions of an array. – rcgldr Oct 14 '20 at 20:35

0 Answers0