median of two sorted arrays

后端 未结 10 621
南旧
南旧 2020-12-16 19:12

My question is with reference to Method 2 of this link. Here two equal length sorted arrays are given and we have to find the median of the two arrays merged.



        
10条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 20:00

     Here is a very simple solution. 
     Actually it need to merger two sorted array and then find the middle.
    
            import java.util.Arrays;
    
    
            public class MedianofTwoArray {
    
                /**
                 * @param args
                 */
                public static void main(String[] args) {
    
                    int []array1= {1,2,3,4,5};
                    int []array2= {6,7,8,9,10};
                    int median;
                    median=findMedian(array1,array2);
                    System.out.println(median);
    
                }
    
                public static int findMedian(int []arr1,int []arr2) {       
                    int [] tempArr=new int[arr1.length+arr2.length]; //creating an array of the length ,equals to sum of arr1 and arr2
                    int i=0;
                    int j=0;
                    int k=0;
    
                    while(i

提交回复
热议问题