Median of Sorted Arrays

Source: Internet
Author: User

There is sorted arrays A and B of size m and N respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (m+n)).

Exercises

First we define what is median, that is, the median.

Refer to Wikipedia's definition of the median:

The method for calculating the median of a finite number of data is to arrange all the same data in the order of size. If the number of data is odd , then the middle data is the median of this group of data, if the number of data is even, then the median of the 2 data is the median of this group of data.

Therefore, when calculating the median median, it needs to be discussed according to the parity classification.

The solution to this problem can be as follows: Looking for a number of unioned in the sorted array (starting from 1). This is equivalent to finding and judging the large number of K/2 (starting from 1) in the two sorted array.

Specialization to median, then for odd, it is the number of M+n/2+1 (starting from 1).

For even numbers, it is the arithmetic mean of the number (M+N)/2 large (starting from 1) and (m+n)/2+1 Large (starting from 1).

So how to judge the number of the k in the two ordered array, a, B?

We need to determine the size of a[k/2-1] and b[k/2-1].

If a[k/2-1]==b[k/2-1], then this number is the number of K in the two array.

If a[k/2-1]<b[k/2-1], then the description a[0] to a[k/2-1] can not be the number of k large, so need to abandon this half, continue from A[K/2] to a[a.length-1] continue to find. Of course, because here abandoned a[0] to a[k/2-1] This K/2 number, then the first k large also became, the first K-K/2 big number.

If a[k/2-1]>b[k/2-1], just do the symmetrical operation before doing it well.

So the whole problem will be solved.

Of course, the boundary condition page should not be less, it is necessary to determine whether there is an array length of 0, and k==1 when the situation.

Because the division is rounded down and the page is taken for convenience, the sub-operation on each array takes:

int parta = Math.min (k/2,m);
int PartB = K-parta;

In order to ensure that the above sub-operation is correct, you need to ensure that the length of a array is less than the length of B array.

At the same time, when returning the result, pay attention to the precision problem, return double type of good.

C + + Implementation code:

#include <iostream>using namespacestd;classSolution { Public:    DoubleFindmediansortedarrays (intA[],intMintB[],intN) {inttotal=m+N; if((m+n)%2)            returnFindmedian (A,0, M-1B0, N-1, total/2+1); Else        {            intPre=findmedian (A,0, M-1B0, N-1, total/2); intLast=findmedian (A,0, M-1B0, N-1, total/2+1); return(pre+last)/2; }    }    DoubleFindmedian (intA[],intAstart,intAend,intB[],intbstart,intBendintk) {intm=aend-astart+1; intn=bend-bstart+1; if(m>N)returnFindmedian (b,bstart,bend,a,astart,aend,k); if(m==0)            returnb[k-1]; if(k==1)            returnmin (A[astart],b[bstart]); intParta=min (m,k/2); intpartb=k-Parta; if(a[astart+parta-1]<b[bstart+partb-1])            returnFindmedian (a,astart+parta,aend,b,bstart,bend,k-Parta); Else if(a[astart+parta-1]>b[bstart+partb-1])            returnFindmedian (a,astart,aend,b,bstart+partb,bend,k-PartB); Else            returna[astart+parta-1]; }};intMain () {solution S; inta[5]= {1,2,3,4,5}; intb[Ten]= {6,7,8,9}; cout<<s.findmediansortedarrays (A,4B4) <<Endl;}

Algorithms that use the complexity of O (m+n):

#include <iostream>using namespacestd;classsolution{ Public:    DoubleFindmediansortedarrays (intA[],intMintB[],intN) {intI=0, j=0, median = m+N; Doubleprev=0, last=0; if(median<2)        {            if(M = =0&& n = =0)return 0; if(m==1)                returna[0]; Else                returnb[0]; }         while((i+j) <= (median/2) ) {prev=Blast; if(I >= m)//if the elements in a are already exhausted, take the B array directly{ Last=B[j]; J++; }            Else if(j>=n)//Ibid .{ Last=A[i]; I++; }            Else if(A[i]<b[j])//A[i] and b[j] in the smaller{ Last=A[i]; I++; }            Else{ Last=B[j]; J++; }        }        if((Median &1) ==0)//an even number of            return(prev + last)/2.0; Else //Odd number of            returnLast ; }};intMain () {solution S; inta[5]= {1,2,3,4,5}; intb[Ten]= {6,7,8,9}; cout<<s.findmediansortedarrays (A,5B4) <<Endl;}

Median of Sorted 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.