int foo(int* a, int* b, int a_min, int a_max, int b_min, int b_max)
{
if((a_max-a_min==1)&&(b_max-b_min==1)) return median of a[a_min],a[a_max],b[b_min],b[b_max];
int a_mid=(a_min+a_max)/2;
int b_mid=ceil((b_min+b_max)/2);
if(a[a_mid]==b[b_mid]) return a[a_mid];
else if(a[a_mid] else return foo(a,b,a_min,a_mid,b_mid,b_max);
}
or
Int foo(int* a, int* b, int a_s, int b_s, int len)//recursively half the len
{
If(len==1) return a[a_s] or b[b_s];
Int m_a=a[a_s+(len-1)/2];
Int m_b=b[b_s+(len-1)/2];
If(m_a>m_b) return foo(a,b,a_s,b_s+len/2,ceil(len/2));
Else return foo(a,b,a_s+len/2,b_s,ceil(len/2));//note when len is ood, ceil is necessary
}
Dec 27, 2008
Find the median value of two sorted arrays.
Find the median value of two sorted arrays.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment