有序数组归并方法
原理:比较数组最前面一个数的大小,小的数拿出来放在新数组中,最后若是某个数组长度很大,就把剩余部分直接放在新数组末尾
/*This is the template of *.cpp files */
#include<iostream>
using namespace std;
int *merge(int *a,int la, int *b, int lb){
int i = 0;
int j = 0;
int k = 0;
int *re = new int[la + lb];
while(i < la && j < lb){
if(a[i] < b[j]){
re[k++] = a[i++];
}else{
re[k++] = b[j++];
}
}
while(i < la)re[k++] = a[i++];
while(j < lb)re[k++] = b[j++];
return re;
}
int main(){
int a[5] = {1,3,5,7,9};
int b[7] = {2,4,6,8,10,56,65};
int *p = merge(a,5,b,7);
for(int i = 0; i < 12; ++i){
cout<<p[i]<<endl;
}
return 0;
}