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