Median "algorithm" for two ordered arrays

Source: Internet
Author: User

There are two ordered arrays A and b, and the design algorithm evaluates the median of a and b.


Situation 1. Arrays A and B are equal in length and set to N.

1) Calculate the median M1 and m2 of A and B, respectively.

2) Compare M1 and M2. If M1 equals M2, then the end result is M1 (M2).

3) If M1 is greater than M2, then the median must be in and two sub-arrays.

4) If M1 is less than M2, then the median must be in and two sub-arrays.

5) Repeat the above steps until the two sub-arrays are 2 in size, then the end result is.


Situation 2. Arrays A and B are unequal in length and are set to M,n (M <= N) respectively.

The assumption is the median, so since the array is ordered,Must be larger than the first number in array a. Moreover, ifIs the median number,Must be larger than the first number in array B. If and, thenis the median, otherwise it can be based onAnd, relationship judgmentis bigger or smaller than the median number. Because array A is ordered, you can use a binary search to find it within the time。 The pseudo code is as follows:

Imin, Imax = 0, mwhile imin <= imax    i = (imin + IMAX)/2    j = ((M + n + 1)/2)-I    if J > 0 and I < M and b[j-1] > a[i]        imin = i + 1    else if i > 0 and J < N and a[i-1] > b[j]        imax = i-1    else        if i = = 0            num1 = b[j-1]        elif j = = 0            num1 = a[i-1]        else            NUM1 = max (a[i-1], b[j-1 ]) if (M + N) & 1:    return num1if i = = m    num2 = B[j]else if J = = n     num2 = a[i]else     num2 = min (A[i], B[J]) return (NUM1 + num2)/2.0


Reference: http://www.geeksforgeeks.org/median-of-two-sorted-arrays/

Https://oj.leetcode.com/discuss/15790/share-my-o-log-min-m-n-solution-with-explanation

http://ocw.alfaisal.edu/NR/rdonlyres/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/ 30c68118-e436-4fe3-8c79-6bafbb07d935/0/ps9sol.pdf

Https://oj.leetcode.com/discuss/11174/share-my-iterative-solution-with-o-log-min-n-m

http://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/



Median "algorithm" for two ordered 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.