0

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. my solution is not working can anyone help

enter code here 
void merge(int* nums1, int m, int* nums2, int n) {
int i=0,j=0,k=0;
while(i<m&&j<n)
{
    if(nums1[i]<nums2[j])
    {
        nums1[k]=nums1[i];
        i++;
        k++;
    }
    else if (nums1[i]>nums2[j])
    {
        nums1[k]=nums2[j];
        k++;
        j++;
    }
    else
    {nums1[k]=nums1[i];
        k++;i++;j++;}
}

while(i!=m)
{  
    nums1[k]=nums1[i];
      k++,i++;


}
while(j!=n)
{
    nums1[k]=nums2[j];
      k++,j++;

}

return nums1;

}

compiler is saying output is wrong.. its a question on leetcode

  • 2
    Possible duplicate of [How to sort in-place using the merge sort algorithm?](https://stackoverflow.com/questions/2571049/how-to-sort-in-place-using-the-merge-sort-algorithm) – Davis Herring Jan 27 '18 at 15:16
  • `enter code here` may have mislead you: `/* replace this by your code */` might be more successful. If this wasn't a duplicate, you should fix your code block by indenting every line following the function head by four more blanks. `[it's] a question on leetcode` provide a link! – greybeard Jan 27 '18 at 15:25
  • https://leetcode.com/problems/merge-sorted-array/description/ – Rohit Sharan Jan 27 '18 at 15:31
  • (don't comment comments asking for additional information or clarification: edit your question.) (As you didn't tag [tag:c], see this [recent post using java](https://stackoverflow.com/questions/48381658/merging-two-sorted-arrays). `compiler is saying output is wrong` that would be the day I quit using compilers.) – greybeard Jan 27 '18 at 15:44
  • "compiler is saying output is wrong that would be the day I quit using compilers." what do u mean by this? – Rohit Sharan Jan 27 '18 at 15:49

1 Answers1

0

The question is that number in nums2 will replace nums1's number when nums2' number is larger, and this will modify numbers in nums1. You can use another buffer to store the result instead of using nums1, or merge them from the end of both.

  • But in the question it is stated that the final array should be saved in nums1?? – Rohit Sharan Jan 27 '18 at 15:29
  • In that case you can calculate the size after merge them and than merge them from the end of both. For example, you have nums1 "4 5 6 7"and nums2 "0 1 2 3", size after merge them is 8. you can start with the end of nums1 "7 "and the end of nums2 "3". As nums1 is bigger, place it at the size 8 of the nums1, then --size and --nums1's index. Actually the idea is the same, just start from the end. – small bottle Jan 27 '18 at 15:47
  • Thanks for your help but can you just change it in code and paste it here please!!! – Rohit Sharan Jan 27 '18 at 15:57
  • void merge(int* nums1, int m, int* nums2, int n) { int i=m-1, j=n-1,k=m+n-1; while(i>=0&&j>=0) { if(nums1[i]>nums2[j]) { nums1[k]=nums1[i]; --i; --k; } else if (nums1[i]<nums2[j]) { nums1[k]=nums2[j]; --k; --j; } else {nums1[k]=nums1[i]; k--,i--,j--;} } while(i>=0) { nums1[k]=nums1[i]; k--,i--; } while(j>=0) { nums1[k]=nums2[j]; k--,j--; } return nums1; Not sure if it's right cause i m in phone, but You should understand the idea from the code. – small bottle Jan 27 '18 at 16:19
  • Thank you so much! i will try using your logic :D .I really appreciate it! – Rohit Sharan Jan 27 '18 at 16:20