Take each k elements from A and B, the kth smallest element is the median of the union of A and B. use binary search to find out median element.
int foo(int* a, int* b, int min_a, int max_a, int min_b, int max_b)//max_a=k, max_b=k
{
if(min_a+1==max_a&&min_b+1==max_b) //last 4 elements, return median of them
return median of a[],a[],b[],b[];
int mid_a=(min_a+max_a)/2;
int mid_b=ceil((min_b+max_b)/2);
if(a[mid_a]==b[mid_b]) return a[mid_a];//find median
if(a[mid_a]>b[mid_b])
return foo(a,b,min_a,mid_a,mid_b,max_b);//cut half
if(a[mid_a] < b[mid_b])
return foo(a,b,mid_a,max_a,min_b,mid_b);//cut half
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment