分治法求中位数
Posted lianggaoblogyuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分治法求中位数相关的知识,希望对你有一定的参考价值。
别人的参考
https://blog.csdn.net/XDU_PYL/article/details/45967535
自己的算法
include
using namespace std;
void Mid(int a[],int aleft,int aright,int b[],int bleft,int bright)
{
double mid;
int n=aright-aleft+1;
int k1=(aleft+aright)/2;
int k2=(bleft+bright)/2;
if(aright-aleft==1&&bright-bleft==1)
{
cout<<"mid="<<(max(a[aleft], b[bleft]) + min(a[aright], b[bright])) / 2.0<<endl;
return ;
}
if((aleft==aright)&&(bleft==bright))
{
mid=(a[aleft]+b[bleft])/2.0;
cout<<"mid="<<mid<<endl;
return;
}
else
{
if(a[k1]==b[k2])
{
mid=a[k1];
cout<<"mid="<<mid<<endl;
}
if(a[k1]<b[k2])
{
Mid(a,k1,aright,b,bleft,k2);
//cout<<"Mid1(a,k1,n-1,b,0,k2)"<<" "<<k1<<" "<<aright<<" "<<" "<<bleft<<" "<<k2<<endl;
}
if(a[k1]>b[k2])
{
Mid(a,aleft,k1,b,k2,bright);
//cout<<"Mid2(a,0,k1,b,k2,n-1)"<<" "<<aleft<<" "<<k1-1<<" "<<" "<<k2<<" "<<bright<<endl;
}
}
}
int main()
{
int a[]={1, 12, 15, 26, 38};
int b[]={13, 14, 40, 42, 45};
Mid(a,0,4,b,0,4);
system("pause");
}
以上是关于分治法求中位数的主要内容,如果未能解决你的问题,请参考以下文章