Welcome to discuss, if there are errors please correct me
If you want to reprint, please specify the source http://www.cnblogs.com/nullzx/
1. Usage scenarios for merge sorting algorithms
The merge sort algorithm and the fast sorting algorithm are the sort calculation used in java.util.Arrays. For the general basic data type, the Arrays.sort function uses a two-axis quick sort algorithm, whereas for object types it uses a merge sort (the Timsort sort algorithm, which is exactly the optimized version of the merge sort). There are two reasons for this, for the first reason, the merge sort is stable, and the fast sort is not stable. For the second reason, the stability of the ordering is not significant for the basic data types, but the stability of the ordering of composite data types (such as objects) can help us maintain some of the properties of the sorting results.
2. Top-down merge sort
The top-down sorting algorithm is to keep the array elements in two, until the number of elements of the subarray is one, because the sub-array must be ordered, then merge two ordered sequences into a new ordered sequence, two new ordered sequences can be merged into another new ordered sequence, and so on, Until it is merged into an ordered array.
To reflect the stability of the sorted sort, our code uses Java generics to sort arbitrary objects.
public static <t extends Comparable<? Super t>> void Mergesortuptodown (t[] A) {@SuppressWarnings ("unchecked")//create auxiliary array combining two ordered sequences t[] aux = (t[]) Array.newinstance (A.getclass (). Getcomponenttype (), a.length); MergeSortUpToDown0 (A, aux, 0, a.length-1);} public static <t extends Comparable<? Super t>> void MergeSortUpToDown0 (t[] A, t[] aux, int start, int end) {if (start = = end) Return;int mid = (start+end)/2 MergeSortUpToDown0 (A, AUX, start, mid), MergeSortUpToDown0 (A, aux, mid+1, end);//copied to the secondary array, at which point [Start,mid] [mid+1, end] Two sub-arrays have been ordered system.arraycopy (A, start, aux, start, end-start + 1);//Then merge back int i = start, j = mid+1, k;for (k = start; K < = END; k++) {if (i > Mid) {a[k] = aux[j++];} ElseIf (J > End) {a[k] = aux[i++];} ElseIf (Aux[i].compareto (aux[j]) <= 0) {A[k] = aux[i++];} ELSE{A[K] = aux[j++];}}
3. Bottom-up merge sort
The idea of a bottom-up merge sort algorithm is that the array first merges into 22 ordered sequences, 22 ordered sequences are merged into four four ordered sequences, four four ordered sequences merge eight sequential sequences, and so on, until the length of the merge is greater than the length of the entire array, the entire array is ordered. It is important to note that the array is divided by the merge length, and the last sub-array may not meet the length requirement, which requires special handling. The merge sort algorithm under the top-down is usually implemented by recursion, and the bottom-up can be realized by loop.
Bottom-up merge sort public static <t extends Comparable<? Super t>> void Mergesortdowntoup (t[] A) {@SuppressWarnings ("unchecked") t[] aux = (t[]) array.newinstance ( A.getclass (). Getcomponenttype (), a.length); int Len,i,j,k,start,mid,end;//len represents the length of the merged sub-array, 1 means that one of the merges, the merged length is 2, 2 represents two two merges, the merged length is 4, and so for (len = 1; len < a.length; len = 2*len) {//Copy to auxiliary array system.arraycopy (A, 0, aux, 0, a.length);// Merge back to the A array according to Len length, the merge length doubles for (start = 0; start < a.length; start = Start+2*len) {mid = start + len-1;//An array of x powers that do not meet 2 for the length of the array, M ID may be greater than EndEnd = Math.min (start + 2*len-1, a.length-1); i = start; When mid is greater than end, J is bound to be greater than end, so it does not cause cross-border access j = Mid+1;//[start,mid] [mid+1, end]for (k = start; k <= end; k++) {if (i > Mid) {a[k] = Aux[j++];} ElseIf (J > End) {a[k] = aux[i++];} ElseIf (Aux[i].compareto (aux[j]) < 0) {A[k] = aux[i++];} ELSE{A[K] = aux[j++];}}}}
4. Reference Articles
[1] Algorithm (fourth edition) Robertsedgewick
Top-down merge sorting and bottom-up merge sort