Given two arrays A and B with the same length and order respectively, calculate the median of the two arrays.

Source: Internet
Author: User

Given two arrays A and B, the length of the array is N, and the two arrays are ordered separately. The median of the two arrays is required.

Analysis: The two arrays have a total of 2n numbers and two medians. Here we take the smaller one. To find the median of 2n numbers, we need to find the nth largest number, and use two subscripts p and q to point to the first element in the arrays A and B respectively, if the element in a is small, P is moved. If the element in B is small, Q is moved, and the number of moves is counted until the nth element is found, total number of moves n-1, time complexity is O (n)

The time complexity of the above method is relatively good, but only one bit is moved at a time. The condition that two arrays are sorted separately is not good. The following provides an O (logn) method. Procedure:

Take the middle elements p and q of array A and array B each time, and compare the two numbers. If P is equal to Q, p and q are the medians of all numbers. If P is greater than Q, then, the median may only exist in the left half of array A and the right half of array B. If P is less than Q, the median may only exist in the right half of array A and the left half of array B, in this way, the scale is half of the original size.

At the same time, it is concluded that the median obtained from arrays A and arrays B is the median of all numbers.

Proof:

Assume that the length of arrays A and B is an odd number of 2 k + 1, take the intermediate element a [k] of array A, and take the intermediate element B [k] of array B, if a [k] is equal to B [K], a [k] Must be 2nd k + 1 large number, which is the median of all numbers. If a [k] is greater than B [K], a [k] must be at least 2nd K + 2 large numbers, and B [k] is mostly 2nd k + 1 large, the median is between B [k] And a [K], so it is impossible to exist in the right half of array A and the left half of array B. The length of the valid array is k + 1, the original number is 4 K + 2, and now it is 2 K + 2, it can be easily proved that the median of these 2 K + 2 numbers is the median of the original 4 K + 2 number, and the problem scale is half of the original, when a [k] is smaller than B [K], the situation is similar.

Assume that the length of array A and array B is an even number of 2 K, each time take the middle element a [k] In the back of array A, take the middle element B in front of array B [k-1], if a [k] is equal to B [k-1], then a [k] must be the 2k largest element, which is the median of all numbers. If a [k] is greater than B [K], then a [k] is at least 2nd k + 1 big number, B [k-1] is at most 2 K big number, the median is between B [k-1] And a [K], so it can only appear in the left half of array A and the right half of array B. The length of the array is k + 1, the original number is 4 K in total and is now 2 K + 2. it can be proved that the median in the number of 2 K + 2 is the median in the original 4 K number, and the problem scale is half of the original number, this is similar when a [k] is less than B [k-1.

When the median is obtained, the length of the array is first determined, and then the problem scale is reduced according to the above two rules. Each time the length is half of the original, so the time complexity is O (logn ).

Note: when the length of the array changes to 2, if A is 67 and 72, and B is 71 and 72, it will be in an endless loop. You need to make another judgment.

 1 # Include <iostream> 2 # Include <assert. h> 3   Using   Namespace  STD;  4  5   Int Midnum ( Int *, Int L1, Int R1, Int * B, Int L2, Int  R2)  6   {  7       Int  Mid1, mid2;  8 Assert (! = NULL & B! = NULL & L1 <= R1 & L2 <= R2 & r1-l1 = R2- L2 );  9       //  When there is only one element left in both arrays, a smaller number is used as the median.  10       If (L1 = R1 & L2 = R2)  11           Return A [L1] <B [L2]? A [L1]: B [L2];  12       //  If only two elements are left in the two arrays, an error occurs. For example, 63 and 72. B are left in. 13       //  At  14       If (R1-l1 = 1  )  15   {  16           If (A [L1] = B [L2])  17               Return  A [L1];  18           Else  If (A [L1] < B [L2])  19               Return B [L2] <A [R1]? B [L2]: A [R1];  20           Else  21               Return A [L1] <B [R2]? A [L1]: B [R2];  22   }  23       //  When the length of the array is an even number, the median in array a is taken from the back, and the rest in array B. 24       //  The median before  25       If (R1-l1 + 1 ) % 2 = 0  )  26   {  27 Mid1 = (R1 + L1 )/ 2 + 1  ;  28 Mid2 = (r2 + l2 )/2  ;  29   }  30       Else  31   {  32 Mid1 = (R1 + L1 )/ 2  ;  33 Mid2 = (r2 + l2 )/ 2  ;  34   }  35      If (A [mid1] = B [mid2])  36           Return  A [mid1];  37       Else   If (A [mid1]> B [mid2])  38           Return  Midnum (A, L1, mid1, B, mid2, R2 );  39       Else  40           Return Midnum (A, mid1, R1, B, L2, mid2 );  41   }  42   43   Int  Main ()  44   {  45       Int A [ 6 ] = { 1 , 3 , 5 , 6 ,8  };  46       Int B [ 6 ] = { 2 , 4 , 6 , 9 , 11  };  47   48       Int M2 = midnum (, 0 , 4 , B, 0 , 4  );  49 Cout <m2 < Endl;  50   51       Int A2 [ 10 ] = { 17 , 18 , 28 , 37 , 42 , 54 , 63 , 72 , 89 , 96  };  52       Int B2 [ 10 ] = { 3 , 51 , 71 , 72 , 91 , 111 , 121 , 131 , 141 , 1000  };  53   54       Int M = midnum (A2, 0 , 9 , B2, 0 , 9  );  55 Cout <m < Endl;  56  57       Return   0  ;  58 }

 

 

 

 

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.