Basic Ideas
Set the current range of Merge Sorting to R [low .. High]. The three steps of division and control are as follows:
① Decomposition: splits the current interval into two parts, that is, finding the split point
② Solution: recursively sort the R [low... mid] and R [Mid + 1 .. High] in two subintervals;
③ Combination: Combine the sorted two subintervals R [low .. mid] and R [Mid + 1 .. high] is merged into an ordered interval r [low .. high].
Recursive termination condition: the subinterval length is 1 (a record is naturally ordered ).
Combine Two ordered sequences into a new ordered sequence.
① Regard N records as N ordered sub-tables with a length of 1;
② Merge the record keywords in an ordered manner to obtain n/2 ordered sub-tables with a length of 2;
③ Repeat Step ② until all records are merged into an ordered table with a length of N.
Important:
1. Implementation of sub-Governance
2. Merge implementation
Divide and conquer means to divide the elements of the entire set into two groups until no two elements are merged.
For Merge Sorting, the key lies in "merge", that is, how to combine two ordered sequences into a new ordered sequence.
Merge implementation
- Defines the variable I, j starting from 0, pointing to the first data of the subsequence A and B respectively;
- Compare the element values at I and j, and copy smaller values to a temporary array.
- If I is small, I ++; otherwise, J ++
Dynamic sorting effect:
Java implementation code:
Package COM. liuhao. sort; import Java. util. arrays;/*** @ author liuhao * JAVA Implementation of Merge Sorting */public class mergesort {public static void mergesort (datawrap [] data) {sort (data, 0, Data. length-1 );} /*** merge and sort the data in the range from left to right of array data * @ Param data to be sorted array * @ Param left * @ Param right */Private Static void sort (datawrap [] data, int left, int right) {If (left <right) {// locate the intermediate index int center = (left + right)/2; // recursive sorting sort (data, left, center) for the array on the left; // recursive sorting sort (data, center + 1, right) for the array on the right ); // merge Merge (data, left, center, right) ;}/ *** to merge the two arrays. The first two arrays are sorted, after merging, it is still ordered * @ Param data array object * @ Param left the first index of the Left array * @ Param center the last element index of the Left array, center + 1 is the first element index of the right array * @ Param right the last element index of the right rent */Private Static void Merge (datawrap [] data, int left, int center, int right) {datawrap [] tmparr = new datawrap [data. length]; // used to store the comparison array. after use, copy it back to the original array int mid = center + 1; // index of the first element in the right array // index of the middle array of the third record int third = left; int TMP = left; while (left <= center & Mid <= right) {// put the smaller values of the two arrays into the temporary array if (data [left]. compareto (data [Mid]) <= 0) {tmparr [third ++] = data [left ++];} else {tmparr [third ++] = data [Mid ++] ;}/// the rest is placed in the middle array in sequence while (mid <= right) {tmparr [third ++] = data [Mid ++];} while (left <= center) {tmparr [third ++] = data [left ++];} // copy the content in the intermediate array to the original array while (TMP <= right) {data [TMP] = tmparr [TMP ++];} public static void main (string [] ARGs) {datawrap [] DATA = {New datawrap (9, ""), new datawrap (-16 ,""), new datawrap (21, ""), new datawrap (23, "*"), new datawrap (-30, ""), new datawrap (-49 ,""), new datawrap (21, ""), new datawrap (30, ""), new datawrap (3, ""), new datawrap (67 ,""), new datawrap (35, ""), new datawrap (5, "")}; system. out. println ("Before sorting:" + arrays. tostring (data); mergesort (data); system. out. println ("sorted:" + arrays. tostring (data ));}}
Code implementation:
Algorithm Analysis
1. Stability
Merge Sorting is a stable sorting.
2. Storage Structure requirements
Available sequential storage structure. It is also easy to implement on the linked list.
3. Time Complexity
For files with a length of N, You need to merge them one by one. The time for each merge is O (n ), therefore, the time complexity is O (nlgn) in both the best and worst cases ).
4. spatial complexity
An auxiliary vector is required to store the results of merging two ordered sub-files. Therefore, the complexity of the auxiliary space is O (n). Obviously, it is not local sorting.
Merge Sorting Based on Sorting Algorithm Summary