天天看點

C語言分治算法求中位數,分治法求中位數(示例代碼)

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<

return ;

}

if((aleft==aright)&&(bleft==bright))

{

mid=(a[aleft]+b[bleft])/2.0;

cout<

return;

}

else

{

if(a[k1]==b[k2])

{

mid=a[k1];

cout<

}

if(a[k1]

{

Mid(a,k1,aright,b,bleft,k2);

//cout<

}

if(a[k1]>b[k2])

{

Mid(a,aleft,k1,b,k2,bright);

//cout<

}

}

}

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");

}