Directory (?) [+] Introduction to merging sort algorithm
Merge sort is a typical example of a partition algorithm (Divide and Conquer), which divides an array into two subgroups of similar size (up to one), and after the array is sorted, a large ordered array is synthesized by merging (merge). The process of merging is still explained by poker, imagine that there are two stacks of cards on the table (from small to large), each one from the two piles inside each one, compare the size of two, if the two pieces of the same large, are taken out into the target array, otherwise take out the smaller set to the target array, the other one put back into the original heap inside. The merge sort requires extra space to store temporary data, but its worst run time is O (nlogn). Java implementation of merging sort algorithm
Like the Java implementation of insert sort (insertsort), implement an Array tool class first. The code is as follows:[Java] View plain copy public class arrayutils { public static void printarray (Int[] array) { system.out.print ("{"); for ( int i = 0; i < array.length; i++) { system.out.print (Array[i]); if (i < array.length - 1) { system.out.print (", "); } } &Nbsp; system.out.println ("}"); } }
The merging sort is divided into the rule,pointsIt is embodied in dividing an array into two sub arrays of basic equal length, repeating this process until the two sub arrays have only one element, the merging process is the core of the algorithm, as mentioned previously. The Java implementation of the merge sort and the test code are as follows:[Java] View plain copy public class mergesort { public static void main (String[] args) { int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3 }; system.out.println ("Before sort:"); arrayutils.printarray (array); MergeSort (array); system.out.println (" After sort: "); arrayutils.printarray (array); } public static Void mergEsort (Int[] array) { sortarray (Array, 0, array.length - 1); } private static void sortarray (Int[] array, int start, int end) { // single element does not need sorting, direct return if (start == end) { return; } int sortSize = end - start + 1; int seperate; if ( sortsize % 2 == 0) { seperate = start + sortSize / 2 - 1; } else { seperate = start + sortSize / 2; } sortarray ( array, start, seperate); sortarray (Array, seperate + 1, end); mergearray (array, start, seperate, end); } private static void mergearray (int[] array, int starT, int seperate, int end) { int totalSize = end - start + 1; int size1 = seperate - start + 1; int size2 = end - seperate; int[] array1 = new int[size1]; int[] array2 = new int[size2]; for (int i = 0; i < size1; i++) { array1[i] = array[start + i]; } for (int i = 0; i < size2; i++) { array2[i] = array[seperate + 1 + i]; } int mergeCount = 0; int index1 = 0; int index2 = 0; while (mergecount < totalsize) { // Check to see if one of the arrays has been processed if (INDEX1&NBSP;==&NBSP;SIZE1) { for (int i = index2; i < size2; i++) { array[start + mergeCount] = array2[i]; mergeCount++; index2++; } } else if (index2 == size2) { &NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; for (int i = index1; i < size1; i++) { array[start + mergecount] = array1[i]; mergeCount++; index1++; } } else { int value1 = array1[index1]; int value2 = array2[index2]; if (value1 == value2) { array[start + mergeCount] = value1; array[start + mergeCount + 1] = value1; mergeCount += 2; index1++; index2++; } else if (value1 < value2) { array[start + mergeCount] = value1; mergeCount++; index1++; } else if (value1 > value2) { array[ start + mergecount] = value2; mergeCount++; index2++; } } } } }