Merge sorting (merge) is the merging of two (or more than two) ordered tables into a new ordered table, which divides the ordered sequence into a number of sub-sequences, each of which is ordered. Then the ordered subsequence is combined into a whole ordered sequence.
Merge sort is an efficient sorting algorithm based on the merging operation. This algorithm is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If you combine two ordered tables into an ordered table, it is called a 2-way merge.
The merge sort algorithm is stable, the array needs O (n) extra space, the list needs O (log (n)) extra space, time complexity is O (Nlog (n)), the algorithm is not adaptive, does not require random reading of the data.
Working principle:
1, the application space, so that its size is the sum of two sorted sequences, the space used to store the combined sequence
2, set two pointers, the initial position is the starting position of two sorted series
3. Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position
4. Repeat step 3 until a pointer reaches the end of the sequence
5. Copy all remaining elements of another sequence directly to the end of the merge sequence
Code implementation:
Public class mergesorttest { public static void main (String[] args) { int [] data = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 }; print (data); mergesort (data); system.out.println ("sorted array:"); print ( data); } public static void mergesort (Int[] data) { Sort (data, 0, data.length - 1); } &nbsP;public static void sort (int[] data, int left, int right) { if (left >= right) return; // Find Intermediate Index int center = (left + right) / 2; // recursive sort to the left array (data, left, center); // recursive to the right array sort (data, center + 1, right); // merger merge (data, lEft, center, right); print (data); } /** * merges two arrays, merges the first 2 arrays in order, and remains orderly after merging * * @param data * Array Objects * @param left * Index of the first element of the left array * @param center * index of the last element of the left array, center+1 is the index of the first element of the right array * @param right * index of the last element of the right array */ public static void merge (int[] data, int left, int center, Int right) { // Temporary array int[] tmpArr = new int[data.length]; // right array first element index int mid = center + 1; // third record the index of a temporary array int third = left; // Caches the index of the first element of the left array int tmp = left; while (left <= center && mid <= right) { // Remove the smallest of the two arrays into the temporary array if ( Data[left] <= data[mid]) { tmpArr[third++] = data[left++]; } else { tmparr[third++] = data [mid++]; } } / / the remainder into a temporary array (actually two whilE will only perform one of them) while (mid <= right) { tmparr[third++] = data[mid++]; } while (Left <= center) { tmpArr[third++] = data[left++]; } // Copy the contents of the temporary array back into the original array // (The contents of the original Left-right range are copied back to the original array) while (tmp < = right) { data[ tmp] = tmparr[tmp++]; } } public static void print (Int[] data) { for (int i = 0; i < data.length; i++) { system.out.print (data[i] + "\ T"); } system.out.println (); } }
Operation Result:
5 3 6 2 1 9 4 8 7 3 5 6 2 1 9 4 8 7 3 5 6 2 1 9 4 8 7 3 5 6 1 2 9 4 8 7 1 2 3 5 6 9 4 8 7 1 2 3 5 6 4 9 8 7 1 2 3 5 6 4 9 7 8 1 2 3 5 6 4 7 8 9 1 2 3 4 5 6 7 8 9 Sorted Array: 1 2 3 4 5 6 7 8 9
Java sorting Algorithm (ix): merge sort