首页 > 编程语言 > 详细

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

时间:2021-05-28 09:58:40      阅读:21      评论: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显然不符合要求

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

原文:https://www.cnblogs.com/AhPuch/p/14820419.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!