将两个顺序数组合并 并查找中位数(时间复杂度低)

时间:2021-06-02 13:49:15   收藏:0   阅读:0

#include<stdio.h>

int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];

int x=1,y=1,tot=0;

while(x<=n&&y<=n){
if(a[x]<b[y]){
ans[++tot]=a[x];
x++;
}else{
ans[++tot]=b[y];
y++;
}
}
while(x<=n){
ans[++tot]=a[x];
x++;
}
while(y<=n){
ans[++tot]=b[y];
x++;
}
return 0;
}

PS:常规方法是冒泡排序 然而时间复杂度为N^2显然不符合要求

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!