Finding the median of the merged array of two sorted arrays in O(logN)?

╄→гoц情女王★ 提交于 2020-01-04 06:45:40

问题


Refering to the solution present at MIT handout

I have tried to figure out the solution myself but have got stuck and I believe I need help to understand the following points.

  1. In the function header used in the solution

    MEDIAN -SEARCH (A[1 . . l], B[1 . . m], max(1,n/2 − m), min(l, n/2))

I do not understand the last two arguments why not simply 1, l why the max and min respectively.

  1. In the pseduo code

    if left > right

    why do we switch A and B arrays if we reach the above condition.

Thanking You.


回答1:


In

MEDIAN-SEARCH(A[1..l], B[1..m], max(1, ceil(n/2)-m), min(l, ceil(n/2)))

the max and min calls restrict the region of A we're searching. We can tell in advance that if a number is at a position less than ceil(n/2)-m in A, then too many elements of A are greater than it for it to be the median. Similarly, a number at a position past ceil(n/2) is greater than too many elements of A to be the median.

If left > right, then the binary search has reduced the segment of A we're searching down to nothing. Switching A and B means we start searching the other array.




回答2:


Well, when

left > right

then, it means median is not present in A. So, it must be present in B. Thus we switch.

For instance, try to work out the algorithm for

A = [ 1, 5] and B = [2, 3, 4]. 

Now, answer is 3. Initially (left, right) is (1, 3). Then it becomes (1, 1) and then (2, 1) now, we switch A and B and continue the procedure on B to get the answer.




回答3:


I have implemented this in JAVA using recursion which has log(m+n) time complexity.

package FindMedianBetween2SortedArraysOfUnequalLength;

import java.util.Arrays;
import java.util.Scanner;

public class UsingKthSmallestElementLogic {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    try{
        System.out.println("Enter the number of elements in the first SORTED array");
        int n = in.nextInt();
        int[] array1 = new int[n];
        System.out.println("Enter the elements of the first SORTED array");
        for(int i=0;i<n;i++)
            array1[i]=in.nextInt();
        System.out.println("Enter the number of elements in the second SORTED array");
        int m = in.nextInt();
        int[] array2 = new int[m];
        System.out.println("Enter the elements of the second SORTED array");
        for(int i=0;i<m;i++)
            array2[i]=in.nextInt();
        System.out.println("Median of the two SORTED arrays is: "+findMedian(array1,array2,array1.length,array2.length));
    }
    finally{
        in.close();
    }
}
private static int findMedian(int[] a, int[] b,
        int aLength, int bLength) { 

    int left = (aLength+bLength+1)>>1;
    int right = (aLength+bLength+2)>>1;
    return ((findKthSmallestElement(a,b,a.length,b.length,left)+findKthSmallestElement(a,b,a.length,b.length,right))/2);
}
private static int findKthSmallestElement(int[] a, int[] b,
        int aLength, int bLength, int k) {                    // All the 5 parameters passed are VERY VERY IMP

    /* to maintain uniformity, we will assume that size_a is smaller than size_b
    else we will swap array in call :) */
    if(aLength>bLength)
        return findKthSmallestElement(b, a, bLength, aLength, k);

    /* We have TWO BASE CASES
     * Now case when size of smaller array is 0 i.e there is no elemt in one array*/
    //BASE CASE 1. If the smallest array length is 0
    if(aLength == 0 && bLength > 0)
            return b[k-1]; // due to zero based index

    /* case where k==1 that means we have hit limit */
    //BASE CASE 2. If k==1
    if(k==1)
            return Math.min(a[0], b[0]);

    /* Now the divide and conquer part */
    int i =  Math.min(aLength, k/2) ; // k should be less than the size of array  
    int j =  Math.min(bLength, k/2) ; // k should be less than the size of array  

    if(a[i-1] > b[j-1])
            // Now we need to find only K-j th element
            return findKthSmallestElement(a, Arrays.copyOfRange(b, j, b.length), a.length, b.length -j, k-j);
    else
            return findKthSmallestElement(Arrays.copyOfRange(a, i, a.length), b, a.length-i, b.length,  k-i);
}
}
/*
Analysis:
    Time Complexity = O(log(n+m))
    Space Complexity = O(1)*/


来源:https://stackoverflow.com/questions/24176431/finding-the-median-of-the-merged-array-of-two-sorted-arrays-in-ologn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!