Recursive and iterative two ways to achieve merge sort (Java edition)

Source: Internet
Author: User

Recursive version
 PackageMergeSort;ImportUtils.sortutils;/** * Merge sort recursive version * @author liguodong */ Public  class Demo02 {     Public Static void MergeSort(int[] a) {Msort (A, A,0, a.length-1); }/** * * @param SR for data to be sorted * @param TR1 for sorted data * @param S * @param /c6> T * /     Public Static void Msort(int[] SR,int[] TR1,intSintT) {intMint[] TR2 =New int[Sr.length];if(s==t)        {Tr1[s] = Sr[s]; }Else{m = (s+t)/2;//4Msort (SR, TR2, S, m); Msort (SR, TR2, m+1, t); Merge (TR2, TR1, S, M, T);//0 4 8}    }//Merge two ordered arrays    /** * @param SR has two ordered arrays * @param TR combines the two ordered arrays of SR into an array TR * @param i * @p Aram m * @param n * *     Public Static void Merge(int[] SR,int[] TR,intIintMintN) {intj,k,l;//i (0~4) J (5~8)         for(j=m+1, K=i; I<=m && j<=n; k++) {if(Sr[i]<sr[j])            {Tr[k] = sr[i++]; }Else{Tr[k] = sr[j++]; }        }if(i<=m) { for(L =0; L <= m-i;            l++) {Tr[k+l] = sr[i+l]; }        }if(j<=n) { for(L =0; L <= n-j;            l++) {Tr[k+l] = sr[j+l]; }        }    } Public Static void Main(string[] args) {int[] A = {2,3,5,4,1,6,9,8,7};        MergeSort (a);    Sortutils.printstring (a); }}

Analysis of Complexity

Iteration Version

 PackageMergeSort;ImportUtils.sortutils;/** * Recursive sort iterations * @author liguodong * */ Public  class Demo03 {     Public Static void MergeSort(int[] a) {int[] TR =New int[A.length];//For storing merge results        intk=1;//Start, sub-sequence length is 1         while(k<a.length) {Mergepass (A, TR, K, a.length);//Merge the previously unordered data 22 into the TRK =2*k;//Sub-sequence length doubledMergepass (TR, A, K, a.length);//To merge the ordered sequence of 22 merges in the TR into the array aK =2*k;//Sub-sequence length doubled}    } Public Static void Mergepass(int[] SR,int[] TR,intSintLen) {intI=0; while(I < len-2*s+1) {//8Merge (sr,tr,i,i+s-1, i+2(So1);//22 Mergei=i+2*s; }//Processing the last Mantissa        //i=8        if(i< len-s+1){//9Merge (SR, TR, I, i+s-1, len-1);//Merge the last two sequences}Else{ for(intj = i; J < Len; J + +) {//If only a single sub-sequence is left at the endTR[J] = Sr[j]; }        }       } Public Static void Merge(int[] SR,int[] TR,intIintMintN) {intj,k,l;//i (0~4) J (5~8)         for(j=m+1, K=i; I<=m && j<=n; k++) {if(Sr[i]<sr[j])            {Tr[k] = sr[i++]; }Else{Tr[k] = sr[j++]; }        }if(i<=m) { for(L =0; L <= m-i;            l++) {Tr[k+l] = sr[i+l]; }        }if(j<=n) { for(L =0; L <= n-j;            l++) {Tr[k+l] = sr[j+l]; }        }    } Public Static void Main(string[] args) {int[] A = {2,3,5,4,1,6,9,8,7,Ten, -, $, +, -, -, to, -, +, at, +, at, +, -, +};        MergeSort (a);    Sortutils.printstring (a); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Recursive and iterative two ways to achieve merge sort (Java edition)

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.