//已知两个排好序的数组A1和A2,内存在A1的末尾有足够多的空间来容纳
//A2,设计算法把A2插入到A1后面,使整体有序
// 归并排序思想,从后往前处理
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iterator>
using namespace std;
void MergeSort(int arr1[], int arr2[],int len1,int len2)
{
if(arr1 == NULL || arr2 == NULL || len1 < 0 || len2 < 0)
return ;
int i,j,k;
int len = len1 + len2;
i = len1 - 1;
j = len2 - 1;
k = len - 1;
while(i >= 0 && j >= 0)
{
if(arr1[i] > arr2[j])
arr1[k] = arr1[i--];
else
arr1[k] = arr2[j--];
--k;
}
while(i >= 0)
{
arr1[k] = arr1[i];
--k;
--i;
}
while(j >= 0)
{
arr1[k] = arr2[j];
--j;
--k;
}
}
int main()
{
int a1[20] = { 1,3,5,8,10};
int a2[5] = {2,4,6,9,12};
MergeSort(a1,a2,5,5);
copy(a1,a1 + 10,ostream_iterator<int>(cout," "));
return 0;
}