Order Table | Two-point lookup: Two median after merging arrays

Source: Internet
Author: User

Enter two ascending arrays of the same length, returning the combined median of the two arrays

C + + code:

intBisearch_midnum (intA[],intB[],intN) {    ints1=0, s2=0, d1=n-1, d2=n-1, m1,m2;  while(S1!=d1 | | s2!=d2) {//as long as the s==d is present in the B sequence, false exitsm1= (S1+D1)/2; M2= (S2+D2)/2;System ("Pause"); if(a[m1]==b[m2])returnA[M1]; Else if(A[M1]<B[M2]) {//∈[M1,M2]D2=m2; if((D1-S1)%2){//evenm1++;//take the middle place.} s1=M1; }Else{//∈[0,m1]∪[m2,n]d1=M1; if((D2-S2)%2){//evenm2++;//take the middle place.} S2=m2; }    }    //return to the lesser person    returnmin (a[s1],b[s2]);}

To understand this problem in one step:

First set of data:

First step: a→←b

(If "s1,d1" is an even number, discard M1, i.e. (M1,D1])

Step Two: A→←b

Step Three: ←a b→

Fourth Step: ←a b→

The median of B is 9 because the "←a b→" situation is to discard the left m.

M1==S1,M2==S2, terminate. In a "S1" and B "S2" to take the smallest, that is, 6.

The median number of two arrays is also 6:

Order Table | Two-point lookup: Two median after merging arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.