Algorithm-two sequential arrays merged into an ordered array

Source: Internet
Author: User

    //merging functions of two ordered arrays     Public Static int[] Mergelist (intA[],intb[]) {        intresult[]; if(Checksort (a) && Checksort (b))//checks if the incoming array is ordered{result=New int[a.length+B.length]; inti=0,j=0,k=0;//I: Used to mark a array J: Used to mark the B array K: Used to mark the incoming array             while(I<a.length && j<b.length)if(A[i] <=B[j]) {Result[k+ +] = a[i++]; }Else{result[k+ +] = b[j++]; }            /*A while loop is used to ensure that the elements in the remaining array are passed through after two arrays are compared .*/             while(I <a.length) Result[k+ +] = a[i++];  while(J <b.length) Result[k+ +] = b[j++]; returnresult; }        Else{System.out.print ("Non-ordered array, not sortable!" "); return NULL; }    }        //checks if the array is stored sequentially     Public Static BooleanChecksort (inta[]) { for(intj=i+1; j<a.length; J + +)                if(A[j-1] >A[j])return false;        }        return true; }        //Print function     Public Static voidPrintintb[]) {          for(inti=0; I<b.length; i++) {System.out.print (B[i]+ (i%10 ==9?) "\ n": "\ T")); }    }         Public Static voidMain (String args[]) {inta[]={1,2,2,3,5,6,7,7}; intb[]={1,2,4,5,8,8,9,10,11,12,12,13,14}; intc[]=Mergelist (A, b); if(c!=NULL) print (c); ElseSystem.out.println (""); }}

Algorithm-two sequential arrays merged into an ordered array

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.