1 Packagecom.iloveu.xxx;2 3 Public classMergeSort {4 5 Static Final intSIZE = 15;6 7 Static voidMergeone (intA[],intB[],intNintlen)8 {9 inti,j,k,s,e;TenS=0; One while(s+len<N) { AE = s+2*len-1; - if(e>=n) {//last segment may be less than Len node -E = n-1; the } - //merging adjacent ordered segments -k=s; -I=s; +j=s+Len; - while(I<s+len && j<=e) {//If two ordered tables are not finished, the loop comparison + if(A[i]<=a[j]) {//if the smaller element is copied into array b Ab[k++]=a[i++]; at}Else{ -b[k++]=a[j++]; - } - } - while(I<s+len) {//The parts that are not merged are copied into array b -b[k++]=a[i++]; in } - while(j<=e) {//The parts that are not merged are copied into array b tob[k++]=a[j++]; + - } thes=e+1;//start subscript for the left segment in the next pair of ordered segments * } $ if(s<n) {//Copy the remaining ordered segment from array A to array bPanax Notoginseng for(; s<n;s++){ -B[s] =A[s]; the } + A } the } + - Static voidMergeSort (intA[],intN//Merge Sort $ { $ inth,count,len,f; - -Count = 0;//Sort Steps thelen = 1;//the length of an ordered sequence -f = 0;//variable f as flagWuyi the int[] p =New int[n]; - while(len<N) { Wu if(f==1) {//alternating between A and P merge -Mergeone (P,a,n,len);//p Merge to a About}Else{ $Mergeone (A,p,n,len);//a merge to P - } -len = len*2;//increase ordered sequence length -F=1-f;//to toggle the F value between 0 and 1 A +count++; theSystem.out.printf ("+count+" step to sort the result: ");//outputs the results of each step sort - for(h=0;h<size;h++){ $System.out.printf ("" +a[h]); the the } theSystem.out.printf ("\ n"); the } - if(f==1) {//If a sort is done in for(h=0;h<n;h++) {//copy data from memory p back to array a thea[h]=P[h]; the } About } the } the the Public Static voidMain (string[] args) { + //TODO auto-generated Method Stub - int[] shuzu=New int[SIZE]; the inti;Bayi the for(i=0;i<size;i++){ theShuzu[i] = (int) (100+math.random () * (100+1));//initializing an array - } - theSystem.out.print ("Array before sorting is: \ n");//array before the output is sorted the for(i=0;i<size;i++){ theSystem.out.print (shuzu[i]+ ""); the } -System.out.print ("\ n"); the theMergeSort (shuzu,size);//Sort Operations the 94System.out.print ("sorted array is: \ n"); the for(i=0;i<size;i++){ theSystem.out.print (shuzu[i]+ "");//output sorted Array the Try {98Thread.Sleep (1000); About}Catch(interruptedexception e) { - //TODO auto-generated Catch block101 e.printstacktrace ();102 }103 }104System.out.print ("\ n"); the }106 107 108}
Package com.iloveu.xxx;
public class MergeSort {static final int SIZE = 15;static void mergeone (int a[],int b[],int n,int len) {int i,j,k,s,e;s=0;w Hile (s+len<n) {e = S+2*len-1;if (e>=n) {///last segment may be less than len node E = n-1;} Adjacent ordered segments Merge K=s;i=s;j=s+len;while (I<s+len && j<=e) {//If two ordered tables are not closed, loop compares if (A[i]<=a[j]) {// If the smaller element is copied into array b b[k++]=a[i++];} Else{b[k++]=a[j++];}} while (I<s+len) {//non-merged parts are copied into array B b[k++]=a[i++];} while (j<=e) {//non-merged parts are copied into array B b[k++]=a[j++];} s=e+1;//the beginning of the left segment of the next pair of ordered segments}if (s<n) {//Copies the remaining ordered segment from array A to array b for (; s<n;s++) {B[s] = A[s];}} static void mergesort (int a[],int N)//merge sort {int h,count,len,f;count = 0;//sort Step len = 1;//ordered sequence length f = 0;//variable f as flag int[] p = new Int[n];while (len<n) {if (f==1) {//alternating between A and P Mergeone (p,a,n,len);//p merge to A}else{mergeone (A,p,n,len);//a merge to P}len = len*2;//increases the ordered sequence length f=1-f;//the F value between 0 and 1 to toggle count++; System.out.printf ("+count+" step to sort results: ");//outputs the result of each step ordering for (h=0;h<size;h++) {System.out.printf (" "" +a[h]);} System.out.printf ("\ n");} if (f==1) {//if sort for (h=0;h<n;h++) {//copies data in memory p back to array aa[h]=p[h];}}}
public static void Main (string[] args) {//TODO auto-generated method stubint[] shuzu=new int[size];int i;for (i=0;i<siz e;i++) {Shuzu[i] = (int) (100+math.random () * (100+1));//Initialize the array}system.out.print ("The array before sorting is: \ n");//output sort before the array for (i=0;i <size;i++) {System.out.print (shuzu[i]+ "");} System.out.print ("\ n"); MergeSort (shuzu,size);//Sort Operation System.out.print ("sorted array is: \ n"); for (i=0;i<size;i++) { System.out.print (shuzu[i]+ "");//output sorted array try {thread.sleep (1000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} System.out.print ("\ n");}
}
Merge sort-java Implementation