Original title
Selection in sorted arrays. Given sorted arrays a[] and b[], of sizes N1 and N2, respectively, design an algorithm to find the kth largest key. The order of growth of the worst case running time of your algorithm should is Logn, where n = n1 + n2.
Version 1:n1 = n2 and k = N/2
Version 2:k = N/2
Version 3:no Restrictions
Analysis:
This topic is asked to search from two ordered arrays to find the K-large element, the visual solution is to merge the number of groups to sort, so the time complexity is O (n), but the topic only requires to know the K-large elements, the rest of the elements than the order of K is not concerned.
The default array A and B are sorted from small to large. Regardless of the k element in a or B, its right element must be greater than k, you can consider excluding the right (K-1)/2 elements from A and b respectively, and the remainder is to find the first (K-excluded Element) large element in the not excluded interval. Gradually narrow the comparison range by recursion. See the following code for the specific algorithm.
1 PackageWeek3;2 3 Importjava.util.Arrays;4 ImportEdu.princeton.cs.algs4.StdRandom;5 6 Public classKthintwosortedarrays {7 /**8 * Find the K-elements9 * @parama array aTen * @paramAlo A's search interval lower bound One * @paramupper bounds of search interval for ahi a A * @paramB Array b - * @paramBlo B's search interval lower bound - * @paramupper bounds of search interval for bhi b the * @paramk large element of the current search interval - * @return - */ - Private Static intFindint[] A,intAlointAhiint[] B,intBlointBhiintk) { + if(Alo > Ahi)returnB[bhi-k+1]; - if(Blo > Bhi)returnA[ahi-k+1]; + if(k==1)returnA[ahi] > B[bhi]?A[ahi]:b[bhi]; A //searching directly with bhi-(K-1)/2 or ahi-(k-1)/2 May leave the K element missing at intBT = bhi-(k-1)/2 > Blo? bhi-(k-1)/2: Blo; - intat = ahi-(k-1)/2 > Alo? ahi-(k-1)/2: Alo; - if(A[at] >=B[BT]) - returnFind (a,alo,at-1,b,blo,bhi,k-(ahi-at+1)); - Else - returnFind (a,alo,ahi,b,blo,bt-1,k-(bhi-bt+1)); in } - to Public Static voidMain (string[] args) { + intn = 10; - intN1 =stdrandom.uniform (n); the intN2 = nN1; * int[] A =New int[N1]; $ int[] B =New int[N2];Panax Notoginseng for(inti=0;i<n1;i++){ -A[i] = stdrandom.uniform (100); the } + for(inti=0;i<n2;i++){ AB[i] = stdrandom.uniform (100); the } + Arrays.sort (a); - Arrays.sort (b); $System.out.println ("a=" +arrays.tostring (a)); $System.out.println ("b=" +arrays.tostring (b)); - int[] C =New int[n]; - inti = 0; the intj = 0; - intL = 0;Wuyi while(l<N) { the if(I>=N1) c[l++] = b[j++]; - Else if(J>=N2) c[l++] = a[i++]; Wu Else{ - if(A[i] <=B[j]) Aboutc[l++] = a[i++]; $ Else -c[l++] = b[j++]; - } - A } +System.out.println ("c=" +arrays.tostring (c)); the - intK =stdrandom.uniform (1, n); $ intLARGESTK = Find (a,0,n1-1,b,0,n2-1, k); theSystem.out.println ("+k+" large element is: "+LARGESTK); the } the}
Coursera algorithms Week3 Quick Sort Exercise quiz: Selection in two sorted arrays (looking for the K-element from both ordered arrays)