码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2021-06-02 13:49:15      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:中位数   cin   turn   include   时间   顺序   ret   ++   数组   

#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显然不符合要求

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

标签:中位数   cin   turn   include   时间   顺序   ret   ++   数组   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!