Find the upper median number __ array in a two-length sorted array

Source: Internet
Author: User

Import java.util.*; Locate the upper median public class findshangmidnum{//Find the top median of two arrays in a two-length sorted array public static int Getshangmidnum (int[]arr1,int[ ]ARR2) {if (arr1==null&&arr2==null) | | (arr1.length==0&&arr2.length==0))
        {throw new RuntimeException ("Your arr is invalid!"); }else if (arr1==null| |
            arr1.length==0) {int temp=0;
            if ((arr2.length&1) ==0)//even {temp=arr2[arr2.length/2-1];
            }else{TEMP=ARR2[ARR2.LENGTH/2];

        return to temp; }else if (arr2==null| |
            arr2.length==0) {int temp=0;
            if ((arr1.length&1) ==0)//even {temp=arr1[arr1.length/2-1];
            }else{TEMP=ARR1[ARR1.LENGTH/2];

        return to temp; int mid= (arr1.length+arr2.length)/2;
        Median/int low=0 to look for;
int Arr1mid=binaryfind (ARR1,LOW,ARR1.LENGTH-1);        int Arr2mid=binaryfind (ARR2,LOW,ARR2.LENGTH-1);
        System.out.println (Arr1mid);
        System.out.println (Arr2mid);
        int re=0;
        int count=arr1.length+arr2.length;

        Int[]arr=new Int[count];
          for (int i=0;i<arr1.length;i++) {arr[i]=arr1[i];
        Arr[arr.length-1-i]=arr2[i];
        } arrays.sort (arr);
    return arr[arr.length/2-1]; }//Method Two (binary lookup method) public static int getShangMidNum02 (INT[]ARR1,INT[]ARR2) {if (arr1==null&&arr2== NULL) | | (arr1.length==0&&arr2.length==0))
        {throw new RuntimeException ("Your arr is invalid!"); }else if (arr1==null| |
            arr1.length==0) {int temp=0;
            if ((arr2.length&1) ==0)//even {temp=arr2[arr2.length/2-1];
            }else{TEMP=ARR2[ARR2.LENGTH/2];

        return to temp; }else if (arr2==null| | arr2.length==0) {intTemp=0;
            if ((arr1.length&1) ==0)//even {temp=arr1[arr1.length/2-1];
            }else{TEMP=ARR1[ARR1.LENGTH/2];

        return to temp;
      int start1=0;
      int end1=arr1.length-1;
      int start2=0;
      int end2=arr2.length-1;
      int mid1=0;
      int mid2=0;
      int offset=0;
         while (START1&LT;END1) {mid1= (START1+END1)/2;
         Mid2= (START2+END2)/2;
         The number of elements is odd, the number of offset=0 elements is even, then Offset=1 offset= ((end1-start1+1) &1) ^1;
            if (Arr1[mid1]>arr2[mid2]) {end1=mid1;
         Start2=mid2+offset;
            }else if (Arr1[mid1]<arr2[mid2]) {start1=mid1+offset;
         End2=mid2;
         }else{return ARR1[MID1];
    } return Math.min (Arr1[start1],arr2[start2]);
        public static void Main (String[]args) {//system.out.println ("Hello");
        int[]arr1={1,2,3,4}; Int[]arr2={3,4,5,6};
        int[]arr2={};
        System.out.println (Getshangmidnum (ARR1,ARR2));
        System.out.println (GETSHANGMIDNUM02 (ARR1,ARR2));
        int[]arr3={0,1,2};
        int[]arr4={3,4,5};
        System.out.println (Getshangmidnum (ARR3,ARR4));


    System.out.println (GETSHANGMIDNUM02 (ARR3,ARR4)); }
}

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.