Merge sort (non-recursive):
public class MergeSort {/** * @param arr array to sort * @param left edge of this merge @param the middle position of this merge in mid, which is the dividing line * @param right boundary of this merge * @param <T> generics * @local aux auxiliary space (auxiliary spaces) */private static <t extends Comparable<? Super t>> void Merge (t[] arr, int left, int mid, Int. right) {t[] aux = Java.util.Arrays.copyOfRange (arr, lef T, right + 1); int i = left; Int J = mid + 1; for (int t = left, T <= right, t++) {if (i > Mid) {arr[t] = aux[j++-left]; } else if (J > right) {arr[t] = aux[i++-left]; } else if (Aux[i-left].compareto (Aux[j-left]) < 0) {Arr[t] = aux[i++-left]; } else {Arr[t] = aux[j++-left]; }}} public static <t extends Comparable<? Super t>> void Sort (t[] arr, int left, int. right) {int n = arr.length; for (int size = 1; Size < n; Size *= 2) {for (int i = 0; i < n-size; i + = size * 2) {merge (arr, I, i + size-1, Math. MIN (i + 2 * size-1, n-1)); }}} public static <t extends Comparable<? Super t>> void Sort (t[] arr) {sort (arr, 0, arr.length-1); } private static void Printarr (object[] arr) {for (Object O:arr) {System.out.print (o); System.out.print ("\ t"); } System.out.println (); } public static void Main (String args[]) {integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; Printarr (arr);//3 5 1 7 2 9 8 0 4 6 sort (arr); Printarr (arr);//0 1 2 3 4 5 6 7 8 9}}Merge sort (non-recursive) optimizations:
Decide if it is necessary to merge before merging
public class MergeSort {/** * @param arr array to sort * @param left edge of this merge @param the middle position of this merge in mid, which is the dividing line * @param right boundary of this merge * @param <T> generics * @local aux auxiliary space (auxiliary spaces) */private static <t extends Comparable<? Super t>> void Merge (t[] arr, int left, int mid, Int. right) {t[] aux = Java.util.Arrays.copyOfRange (arr, lef T, right + 1); int i = left; Int J = mid + 1; for (int t = left, T <= right, t++) {if (i > Mid) {arr[t] = aux[j++-left]; } else if (J > right) {arr[t] = aux[i++-left]; } else if (Aux[i-left].compareto (Aux[j-left]) < 0) {Arr[t] = aux[i++-left]; } else {Arr[t] = aux[j++-left]; }}} public static <t extends Comparable<? Super t>> void Sort (t[] arr, int left, int. right) {int n = arr.length; for (int size = 1; Size < n; Size *= 2) {for (int i = 0; i < n-size; i + = size * 2) {if (Arr[i + Size-1].compareto (a Rr[i + size]) > 0) {merge (arr, I, i + size-1, math.min (i + 2 * size-1, n-1)); }}}} public static <t extends Comparable<? Super t>> void Sort (t[] arr) {sort (arr, 0, arr.length-1); } private static void Printarr (object[] arr) {for (Object O:arr) {System.out.print (o); System.out.print ("\ t"); } System.out.println (); } public static void Main (String args[]) {integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; Printarr (arr);//3 5 1 7 2 9 8 0 4 6 sort (arr); Printarr (arr);//0 1 2 3 4 5 6 7 8 9}}Recursive ordering (non-recursive) continuation optimization: Using insert sorting for small-scale data
Merge sort is the aggregation of a group of data. When the number in this group is very small (tentatively 4), insert sort is used.
public class MergeSort {/** * @param arr array to sort * @param left edge of this merge @param the middle position of this merge in mid, which is the dividing line * @param right boundary of this merge * @param <T> generics * @local aux auxiliary space (auxiliary spaces) */private static <t extends Comparable<? Super t>> void Merge (t[] arr, int left, int mid, Int. right) {t[] aux = Java.util.Arrays.copyOfRange (arr, lef T, right + 1); int i = left; Int J = mid + 1; for (int t = left, T <= right, t++) {if (i > Mid) {arr[t] = aux[j++-left]; } else if (J > right) {arr[t] = aux[i++-left]; } else if (Aux[i-left].compareto (Aux[j-left]) < 0) {Arr[t] = aux[i++-left]; } else {Arr[t] = aux[j++-left]; }}} private static <t extends Comparable<? Super t>> void Insertionsort (t[] arr, int left, int. right) {for (int i = left + 1; i<= right; i++) {T temp = arr[i]; int j = i-1; while (J >= left && Temp.compareto (Arr[j]) < 0) {arr[j + 1] = Arr[j]; j--; } arr[j + 1] = temp; }} public static <t extends Comparable<? Super t>> void Sort (t[] arr, int left, int. right) {int len = arr.length; int smallsize = 4;//when size is less than 4 o'clock with insert sort for (int i = 0; i < len; i + = smallsize) {insertionsort (arr, I, Ma Th.min (i + smallSize-1, len-1)); } for (int size = smallsize; size < len; size *= 2) {for (int i = 0; i < len-size; i + = size * 2) {if (Arr[i + size-1].compareto (arr[i + size]) > 0) {merge (arr, I, i + size-1, Math.min (i + 2 * size-1, len-1)); }}}} public static <t extends Comparable<? Super t>> void Sort (t[] arr) {sort (arr, 0,ARR.LENGTH-1); } private static void Printarr (object[] arr) {for (Object O:arr) {System.out.print (o); System.out.print ("\ t"); } System.out.println (); } public static void Main (String args[]) {integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; Printarr (arr);//3 5 1 7 2 9 8 0 4 6 sort (arr); Printarr (arr);//0 1 2 3 4 5 6 7 8 9}}
Merge sort (non-recursive, Java implementation)