Gamefrye Merge sort is an effective sorting algorithm based on merging operation, which is a very typical application using divide-and-conquer 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 two ordered tables are combined into an ordered table, they are called two-way merging.
The basic idea of merging sort
The ordered sequence r[0...n-1] is treated as an ordered sequence with n length of 1, the adjacent ordered table is merged into pairs, and the ordered table with N/2 length 2 is obtained, and the ordered sequences are merged again, and the ordered sequence of N/4 length is 4. So repeated, and finally get an ordered sequence of length n.
In conclusion:
Merge sort actually two things to do:
(1) "decomposition"--divides the sequence binary each time.
(2) "Merge"--sorts after the divided sequence segment 22 is merged.
Let's consider the second step, how to merge?
During each merge process, the two ordered sequence segments are merged and then sorted.
These two ordered sequence segments are R[low, mid] and r[mid+1, high].
Merge them into a partial staging array R2, with the merge complete before copying the R2 back into R.
To facilitate the description, we call R[low, mid] the first paragraph, r[mid+1, high] as the second paragraph.
Each time a record is taken from two segments to compare the keywords, the smaller ones are placed in the R2. Finally, the remainder of the paragraphs are copied directly into the R2.
After this process, the R2 is already an ordered sequence, and then it is copied back to R, and a merge sort is completed.
Core code:
public void Merge (int[] Array, int. low, int mid, Int. high) {
int i = low; I is the subscript of the first paragraph sequence
Int J = mid + 1; J is the subscript for the second segment of the sequence
int k = 0; K is the subscript that temporarily holds the merged sequence
int[] Array2 = new Int[high-low + 1]; Array2 is a temporary merge sequence
//Scan the first and second sequences until a scan ends
while (I <= mid && J <= High) {
&nb sp; //Determine which of the first and second paragraphs is smaller, save it in the merge sequence, and continue to scan down
if (Array[i] <= array[j]) {
Array2[k] = array[i];
i++;
k++;
} else {
Array2[k] = array[j];
J + +;
k++;
}
}
If the first sequence has not been scanned, copy it all to the merge sequence
while (I <= mid) {
ARRAY2[K] = Array[i];
i++;
k++;
}
If the second sequence has not been scanned, copy it all to the merge sequence
while (J <= High) {
ARRAY2[K] = Array[j];
j + +;
k++;
}
Copy a merge sequence into the original sequence
Huai ' An whipped egg
for (k = 0, i = low; I <= high; i++, k++) {
Array[i] = array2[k];
}
}
This article is from the "Flying Pigeon biography, Flying Pigeon Book 2012:" blog, please be sure to keep this source http://ipmsg.blog.51cto.com/1051763/1618844
Merge sort is an effective sort algorithm