PHP two equi-ordered arrays to calculate the merged Median

Source: Internet
Author: User

Algorithm idea: Compare the medians of two arrays each time,

We assume that the median is the lower median, that is, the floor is used to retrieve the median coordinate each time. When the array length is an odd number, the elements in the median are obtained. If the array length is an even number, the lower median is obtained.

1. A [n/2] = B [n/2], we can see that the median is a [n/2], and return.

2. A [n/2] <B [n/2], set the blue and red elements of array A and array B to a [B], a [R], B [B], B [R],

We can see that a [B] <= A [n/2] <B [n/2] <= B [B]

So there is a [B] <= A [R], a [B] <B [B] (array elements may already exist)

B [B]> = B [R], B [B]> A [B]

In this way, we know that the elements in a [B] are at least less than n elements, and the elements in B [B] are at least more than n elements, so the approximate median cannot be in the blue part.

After the blue part is excluded, you can re-use the median method for the red part to find the total median until only one element is left in the split array, let's take a smaller element.

So when we split the array from the median coordinate, do we need to put the median element into the red part? There are two cases

1) WHEN n is an odd number, we need to put the median of A and B in the red part for recursive search, because after the arrays A and B are merged, A [n/2] B [n/2] may still be placed in the center of the merged array, so the median is one of the two.

For example, if a = (, 9), B = (, 10), it must be, it can be seen that 5 and 6 are medians.

2) When n is an even number, we need to exclude the smaller median, and only let the red part on the right of the N into recursive search (Here we exclude a [n/2], if a [n/2]> B [n/2], B [n/2] is excluded.

This is because if the array length is an even number, the median we take must be the lower median, and a [n/2] <B [n/2], after the array is merged, A [n/2] is definitely on the left of the total median, so a [n/2] is definitely not the last median.

For example, if a = (12345678,), B = (,), then the result is. The median value is 3, 4, it can be seen that 3 is not the median, and the median is 4, 5.

 

Okay. See the code below

1 <? PHP 2 # The two arrays with N lengths are sorted. Calculate the median of all numbers in the two arrays. 3 function get_mid ($ A, $ B) {4 print_r ($ A); 5 echo "<br>"; 6 print_r ($ B); 7 Echo "<br>"; 8 If (count ($) = 1) {9 $ result_mid = min ($ A [0], $ B [0]); # If the array contains only one element, returns the median 10} else {11 $ mid = floor (count ($ A)-1)/2); # removes the median 12 echo "mid: {$ mid} <br> "; 13 14 # If a [Mid] = B [Mid], the median is MIDA, return 15 # If a [Mid] <B [Mid] is returned, select the right part of array A and the left part of array B to go to recursion until the last recursion part of the array length is 116 # as shown in Figure If a [Mid]> B [Mid] is selected, the left part of array a is selected, and the right part of array B is recursive until the last part of the recursive array is 117 in length. # note that, when the array length is an odd number, the median of the AB array must be 19 # When the array length is an even number, you only need to add the median to the left of the larger array, for a smaller array, the median is discarded as long as the right side enters the recursion 20 21 if (count ($ A) % 2 = 0) {22 $ small_arr_start = $ Mid + 1; 23} else {24 $ small_arr_start = $ mid; 25} 26 27 if ($ A [$ mid] = $ B [$ mid]) {# the median of the two arrays is equal, returns the median 28 $ result_mid = $ A [$ mid]; 29} else if ($ A [$ mid] <$ B [$ mid]). {# note 30 $ atemp = array_slice ($ A, $ smal Rochelle arr_start); # split the array. A smaller array discards the median 31 $ btemp = array_slice ($ B, 0, $ Mid + 1) based on whether the length is parity ); 32 $ result_mid = get_mid ($ atemp, $ btemp); 33} else {34 $ atemp = array_slice ($ A, 0, $ Mid + 1 ); 35 $ btemp = array_slice ($ B, $ small_arr_start); 36 $ result_mid = get_mid ($ atemp, $ btemp); 37} 38} 39 40 return $ result_mid; 41} 42 43 $ A = array (1, 3, 5, 5, 7, 8, 9); 44 $ B = array (2, 2, 7, 8, 9, 10, 14); 45 46 echo "midnum :". get _ Mid ($ A, $ B ). "<br>"; 47 48 array_splice ($ A, count ($ A)-1, 0, $ B); 49 sort ($ ); 50 51 print_r ($ A); 52?>

Array ([0] => 1 [1] => 3 [2] => 5 [3] => 5 [4] => 7 [5] => 8 [6] => 9)
Array ([0] => 2 [1] => 2 [2] => 7 [3] => 8 [4] => 9 [5] => 10 [6] => 14)
Mid: 3
Array ([0] => 5 [1] => 7 [2] => 8 [3] => 9)
Array ([0] => 2 [1] => 2 [2] => 7 [3] => 8)
Mid: 1
Array ([0] => 5 [1] => 7)
Array ([0] => 7 [1] => 8)
Mid: 0
Array ([0] => 7)
Array ([0] => 7)
Midnum: 7
Array ([0] => 1 [1] => 2 [2] => 2 [3] => 3 [4] => 5 [5] => 5 [6] => 7 [7] => 7 [8] => 8 [9] => 8 [10] => 9 [11] => 9 [12] => 10 [13] => 14)

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.