Find the median number of two arrays

Source: Internet
Author: User

 Packagekpp.base;/*** Find the median of two ordered arrays * This code applies to cases where the length of the two arrays is unequal *@authorKPP **/ Public classTwoarraymedian { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        floatA[] = {1,2,3,4}; //float b[] = {2,3,4,5,8,9,10};        floatB[] = {8,9,10,11}; System.out.println ("Median" +Gettwoarraymedian (A, b)); }    Private Static floatGettwoarraymedian (floatA[],floatb[]) {        intALen =a.length; intBlen =b.length; floatshorterarray[] = ALen <= blen?a:b; floatlongerarray[] = aLen > Blen?a:b; System.out.println ("Shorterarray");  for(inti = 0;i < shorterarray.length;i++) {System.out.print (Shorterarray[i]+" ");                } System.out.println (); System.out.println ("Longerarray");  for(inti = 0;i < longerarray.length;i++) {System.out.print (Longerarray[i]+" ");                } System.out.println ();  while(Shorterarray.length > 1 && longerarray.length > 1){            if(Getarraymedian (Shorterarray) >Getarraymedian (Longerarray)) {                floatTemp1[] =New float[Shorterarray.length/2+1]; System.arraycopy (Shorterarray,0, Temp1, 0, shorterarray.length/2+1); System.out.println ("Temp1");  for(inti = 0;i < temp1.length;i++) {System.out.print (Temp1[i]+" ");                                } System.out.println (); intDelcount = shorterarray.length-shorterarray.length/2-1; floatTemp2[] =New float[longerarray.length-Delcount]; System.arraycopy (Longerarray, Delcount-1, Temp2, 0,longerarray.length-delcount); System.out.println ("Temp2");  for(inti = 0;i < temp2.length;i++) {System.out.print (Temp2[i]+" ");                                } System.out.println (); Shorterarray=Temp1; Longerarray=Temp2; }Else if(Getarraymedian (Shorterarray) <Getarraymedian (Longerarray)) {                floatTemp1[] =New float[SHORTERARRAY.LENGTH-SHORTERARRAY.LENGTH/2]; System.arraycopy (Shorterarray, Shorterarray.length/2, Temp1, 0, SHORTERARRAY.LENGTH-SHORTERARRAY.LENGTH/2); System.out.println ("Temp1");  for(inti = 0;i < temp1.length;i++) {System.out.print (Temp1[i]+" ");                                } System.out.println (); intDelcount = SHORTERARRAY.LENGTH/2; floatTemp2[] =New float[longerarray.length-Delcount]; System.arraycopy (Longerarray,0, Temp2, 0,longerarray.length-delcount); System.out.println ("Temp2");  for(inti = 0;i < temp2.length;i++) {System.out.print (Temp2[i]+" ");                                } System.out.println (); Shorterarray=Temp1; Longerarray=Temp2; }Else{                returnGetarraymedian (Shorterarray); }                    }                //inserts an array of the remaining elements into another array        if(Longerarray.length > 1){            floatKey = Shorterarray[0]; floatRsarray[] =New float[Longerarray.length+1]; System.arraycopy (Longerarray,0, Rsarray, 0, longerarray.length); Rsarray[rsarray.length-1] =key; System.out.println ("Key" +key); System.out.println ("Rsarray");  for(inti = 0;i < rsarray.length;i++) {System.out.print (Rsarray[i]+" ");                        } System.out.println (); if(Key < Rsarray[rsarray.length-2]){                floattemp =key; intj = 0;  for(j = Rsarray.length-2;j >=0&&temp < rsarray[j];j--); //Unify Move Right                 for(intK = rsarray.length-2;k >= j+1;k--) {rsarray[k+1]=Rsarray[k]; }                //Insert the correct positionRSARRAY[J+1] =temp; } System.out.println ("Rsarray after sorting");  for(inti = 0;i < rsarray.length;i++) {System.out.print (Rsarray[i]+" ");                        } System.out.println (); returnGetarraymedian (Rsarray); }Else{            return(Shorterarray[0]+longerarray[0])/2; }                    }    /*** Ask for the median of an array *@paramData *@return     */    Private Static floatGetarraymedian (floatdata[]) {        intLen =data.length; intMID = Len/2; if(len%2 = = 0) {System.out.println ("Interim Median" + (data[mid]+data[mid-1])/2); return(Data[mid]+data[mid-1])/2; }Else{            returnData[mid]; }    }    }

Find the median number of two 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: 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.