This is a Natural the Merge Sort program from my textbook. It works, but I don ' t think it ' s good.
//Natural merge sort program in the textbook
Public classNaturalmergesortprogram { Public Static voidMain (string[] args) {intA[] =New int[10000000]; intB[] =New int[A.length]; for(inti = 0; i < a.length; i++) A[i]= (int) (1+math.random () * (100-0+1)); LongStartime=System.currenttimemillis (); Naturalmergesort (A, b); LongEndtime=System.currenttimemillis (); LongTime = EndTime-Startime; System.out.println ("Executing time:" +time+ "(ms)"); /*for (int i = 0; i < b.length; i++) {if (i% = = 0) System.out.println (); System.out.print (a[i]+ ""); }*/ } Public Static voidNaturalmergesort (intA[],intb[]) { //Merge Array A to B and then b into a until sorted while(! Mergeruns (A, b) &!Mergeruns (b, a)); } Public Static BooleanMergeruns (intA[],intb[]) { inti = 0, k = 0; intn =a.length; BooleanASC =true; intx; while(I <N) {k=i; Dox= a[i++]; while(I < n && x <= a[i]);//Elements is increasing while(I < n && x >= a[i])//Elements is decreasingx = a[i++]; Merge (A, B, K, I-1, ASC); ASC= !ASC; } returnK = = 0; } Public Static voidMergeintA[],intB[],intLowintHighBooleanASC) { //merge A[low:high] into B[low:high] intk = asc?Low:high; intc = ASC? 1:-1; inti = low, j =High ; while(I <=j) {if(A[i] <=A[j]) b[k]= a[i++]; ElseB[k]= a[j--]; K+=C; } }}
Or maybe I don ' t get it? Because it ' s rather obscure and lack of comments (these comments is all added by me).
So I decide to the write my own Natural Merge Sort program:
My own natural Merge sort program
Public classMynaturalmergesort { Public Static voidMain (String args[]) {intA[] =New int[10000000]; intB[] =New int[A.length]; for(inti = 0; i < a.length; i++) A[i]= (int) (1+math.random () * (100-0+1)); LongStartime=System.currenttimemillis (); while(! Naturalmergesort (A, b) &!Naturalmergesort (b, a)); LongEndtime=System.currenttimemillis (); LongTime = EndTime-Startime; System.out.println ("Executing time:" +time+ "(ms)"); /*for (int i = 0; i < a.length; i++) System.out.print (a[i]+ ""); System.out.println (); */ } Public Static BooleanNaturalmergesort (intX[],inty[]) { //find the adjacent natural increasing arrays x[l:m] and x[m+1:r],//Then merge them to Y[l:r] using function merge () intI, l, m = 0, R; for(i = 0; i < x.length; i++) {L=i; while((I < x.length-1) && (X[i] <= x[i+1]))//get x[l:m]i++; M= i++; if(m = = x.length-1)//The whole array is sorted return true; while((I < x.length-1) && (X[i] <= x[i+1]))//get X[m+1:r]i++; R=i; Merge (x, Y, L, M, R); } return false;//The whole array is not yet sorted } Public Static voidMergeintX[],intY[],intLintMintr) {//merge x[l:m] and X[m+1:r] into Y[l:r] inti =L, J= M+1, K=l; while((i <= m) && (J <=R))if(X[i] <=X[j]) y[k+ +] = x[i++]; Elsey[k+ +] = x[j++]; while(k <=R)if(i > M)//elements in X[l:m] is all merged to array y[]y[k++] = x[j++]; Elsey[k+ +] = x[i++]; }}
After running 3 times, I got the executing time as below:
Program in textbook--1205ms 1261ms 1260ms
My program--202ms 203ms 198ms
wow! My program is bad-ass! At least better than, the one in textbook. This makes me exciting!
And through this practice, I came to know there's a lot of fun hacking the algorithm. I ' m looking forword to write more beautiful and efficient code!
Natural Merge sort (sorted by natural merge)