Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
A和B都已经是排好序的数组,我们只需要从后往前比较就可以了。因为A有足够的空间容纳A + B,我们使用游标i指向m + n - 1,也就是最大数值存放的地方,从后往前遍历A,B,谁大就放到i这里,同时递减i。
代码如下:
#include<iostream>
using namespace std;
//返回合并后数组的指针
int *merge(int a[],int m,int b[],int n)
{
int i=m+n-1;
int j=m-1;
int k=n-1;
while (i>=0)
{
if (j>=0&&k>=0)
{
if (a[j]>b[k])
{
a[i]=a[j];
j--;
}
else
{
a[i]=b[k];
k--;
}
}
else if (j>=0)
{
a[i]=a[j];
j--;
}
else
{
a[i]=b[k];
k--;
}
i--;
}
return a;
}
void main()
{
int m=5,n=6;
int a[11]={1,3,5,7,9};
int b[6]={2,4,6,8,10,12};
int* c=merge(a,m,b,n);
cout<<"合并后的数组为:";
for (int i=0;i<m+n;i++)
{
cout<<c[i]<<" ";
}
system("pause");
}