Median of Sorted Arrays

Source: Internet
Author: User

1. Question

Find the median number of two ordered arrays. Time Complexity O (log (m+n))

There is sorted arrays nums1 and nums2 of size m and N respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (m+n)).
2. Solution2.1 O (NLOGN)

Copy two arrays to a new array and sort the new array

1  Public classSolution {2      Public DoubleFindmediansortedarrays (int[] A,int[] b) {3         int[] C =New int[A.length +B.length];4System.arraycopy (A, 0, C, 0, a.length);5System.arraycopy (b, 0, C, A.length, b.length);6 Arrays.sort (c);7         intMID = C.length/2;8         if(c.length% 2 = = 0 )9             return(C[mid-1] + C[mid])/2.0;Ten         returnC[mid]; One     } A}

2.2 O (N/2)---->o (n)

Merges two arrays.

1  Public classSolution {2      Public DoubleFindmediansortedarrays (int[] A,int[] b) {3         intLen1 =a.length;4         intLen2 =b.length;5         intLen = (len1 + len2)/2 + 1;6         int[] C =New int[Len];7         intia;8         intIB;9         intIC;Ten          for(Ia=0, ib=0, ic=0; ia<len1 && ib<len2 && ic<len; ic++ ){ One             if(A[ia] <=B[ib]) AC[ic] = a[ia++]; -             Else -C[ic] = b[ib++]; the         } -          for(; ia<len1 && ic<len; ic++, ia++ ) -C[ic] =A[ia]; -          for(; ib<len2 && ic<len; ic++, ib++ ) +C[ic] =B[ib];  -ic--; +         if((len1 + len2)% 2 = = 0 ) A             return(C[ic] + c[ic-1])/2.0; at         returnC[ic]; -     } -}

2.3 O (LOGN)

Define the following variables:

    • Median of m1:nums1 (with M/2 number less than or equal to M1)
    • Median of M2:NUMS2 (with N/2 number less than or equal to m2)

Judge the size of M1 and M2 to discard half of the array:

    • M1<M2: There are (m+n)/2 number <=m2, that is, M1 must exist in the first half of the merged array, and M2 exists in the second part. The median is present in the second half of the NUMS1 or the first half of the NUMS2;
    • M1>M2: Ibid., median exists in the first half of the NUMS1 or the second half of the NUMS2;
    • M1 = = M2:m1 is the median
1  Public classSolution {2     //find the kth big number;3      Public intFindkth (int[] A,intAfrom,intAtoint[] B,intBfrom,intbtointk) {4         //we assume that m<=n5         intm = ato-afrom + 1;6         intn = bto-bfrom + 1;7         if(M >N)8             returnfindkth (b, Bfrom, BTO, A, Afrom, ATO, K);9         if(M = = 0  )Ten             returnB[k-1]; One         if(k==1 ) A             return(A[afrom] < B[bfrom])?A[afrom]: b[bfrom]; -         intNa = (K/2 < m)? K/2: M; -         intNB = kna; the         intPA = Na + afrom-1; -         intPB = nb + bfrom-1; -         if(A[pa] >B[PB]) -             returnFindkth (A, Afrom, PA, B, Pb+1, BTO, Knb); +         if(A[pa] <B[PB]) -             returnFindkth (A, pa+1, ATO, B, Bfrom, Pb, Kna); +         returnB[PB]; A     } at      -     //O (log (m+n)) time -      Public DoubleFindmediansortedarrays (int[] A,int[] b) { -         intLen = A.length +b.length; -         if(len% 2 = = 0 ) -             return(Findkth (A, 0, a.length-1, b, 0, b.length-1, LEN/2) + findkth (A, 0, a.length-1, b, 0, b.length-1, len/2+1))/2.0; in         Else  -             returnFindkth (A, 0, a.length-1, b, 0, b.length-1, len/2+1 ); to     } +}

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: 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.