Overview
The merge (merge) Sort method combines two (or more) ordered tables into a new ordered table, which divides the sequence into several subgroups, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.
The merge sort uses the recursive return implementation, belong to "divide and conquer", the target array is divided in the middle, after the two arrays are sorted, after sorting, the sequence of two arrays "merged" together, the most important merge sort is the "merge" process, The merging process requires additional space that is consistent with the two number of group leaders that need to be merged.
Effect Chart:
Steps
Application space so that the sum of the two sorted sequences is used to store the merged sequence
Set two pointers, starting at the beginning of two sorted sequences, respectively
Compares the elements pointed to by two pointers, selects the relatively small elements into the merged space, and moves the pointer to the next position
Repeat step 3 until a pointer reaches the end of the sequence
Copy all remaining elements of another sequence directly to the end of the merge sequence
Instance
Raw data:
3 5 2 6 2
The premise of merging is to separate the array, split in Split, and then divided into no more points to merge.
First-round dividers, index 2 ((0+4)/2=2) for the middle
Second-round separation, separating [3 5 2]
Third-round separation, separate for [3 5]
merging [3] [5]
Merging [3 5] [2]
Fourth-round separation, separating for [6 2]
merging [6] [2]
Merge [2 3 5] [2 6]
Code implementation (Java)
Package com.coder4j.main.arithmetic.sorting;
public class Merge {private static int mark = 0; /** * Merge Sort * @param array * @param low * @param high * @return/private static int[] sort (int[
array, int low, int high) {int mid = (low + high)/2;
if (Low < high) {mark++;
System.out.println ("in progress" + Mark + "secondary separation, get");
System.out.println ("[" + Low + "-" + mid + "] [" + (mid + 1) + "-" + High + "]");
Left array sort (array, low, mid);
Right array sort (array, mid + 1, high);
Merge merges around (array, low, mid, high);
} return array; /** * Array * @param array * @param low * @param mid * @param high */private static void Merge (int[] array, int low, int mid, int high) {System.out.println ("merged: [+ Low +"-"+ mid +"] and [] + (mid + 1) +
"-" + High + "]");
int[] Temp = new Int[high-low + 1]; int i = low;//left pointer int j = mid + 1;//right pointer int k = 0; Move the smaller number first to the new array while (I <= mid && J <= High) {if (Array[i] < array[j]) {temp[k++] =
Array[i++];
else {temp[k++] = array[j++];
One of the two arrays may have remaining elements//move the left remaining number into the array while (I <= mid) {temp[k++] = array[i++];
///Move the remaining number on the right side into the array while (J <= High) {temp[k++] = array[j++];
(int m = 0; m < temp.length m++) {array[m + low] = Temp[m] in the new array; /** * Merge Sort * * @param array * @return/public static int[] Sort (int[] array) {return sor
T (array, 0, array.length-1);
public static void Main (string[] args) {int[] array = {3, 5, 2, 6, 2};
int[] sorted = sort (array);
SYSTEM.OUT.PRINTLN ("final result");
for (int i:sorted) {System.out.print (i + "");
}
}
}
Test output results:
A 1th separation is in progress, the 2nd separation is being
made [0-2] [3-4], and the [
0-1] [2-2]
is undergoing a 3rd separation, resulting in
[0-0] [1-1]
merging: [0-0] and [1-1]
merge: [0-1] and [2-2]
are being separated for the 4th time, get
[3-3] [4-4]
merge: [3-3] and [4-4]
merge: [0-2] and [3-4]
end result
2 2 3 5 6
Tested, consistent with the results in the example.