Two-way merge sort of algorithm

Source: Internet
Author: User

 Public classSorttest {Private Static intarray_number=10;  Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//prepare the sequence to be sortedRandom random =NewRandom (); int[] Array =New int[Array_number];  for(inti = 0; i < Array.Length; i++) {Array[i]= Random.nextint (20); } System.out.println ("The initial ordering of the array is as follows: \ n");        System.out.println (arrays.tostring (array)); MergeSort (Array,0,array.length-1); System.out.println ("The merge of arrays is sorted as follows: \ n");    System.out.println (arrays.tostring (array)); }    //Two-way merge sort    Private Static voidMergeSort (int[] list,intLeftintRight ) {        if(Left <Right ) {            intMid = (left+right)/2;            MergeSort (List,left,mid); MergeSort (List,mid+1, right);        Mergeration (List,left,mid,right); }    }    Private Static voidMergeration (int[] list,intLeftintMidintRight ) {        //TODO auto-generated Method Stub        inti,j,k; int[] temp =New int[Right-left+1]; K= 0; I=Left ; J=mid+1;  while(i<=mid&&j<=Right ) {            if(List[i] <=List[j]) {Temp[k+ +] = list[i++]; }Else{temp[k+ +] = list[j++]; }        }         while(i<=mid) {Temp[k+ +] = list[i++]; }         while(j<=Right ) {Temp[k+ +] = list[j++]; }        //to copy back into the corresponding segment of the original array after the sequence is sorted         for(i=0;i<temp.length;i++) {List[left+i]=Temp[i]; }    }}

Two-way merge sort of algorithm

Related Article

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.