The merge sort takes the recursive implementation, belongs to "Divide and conquer", divides the target array from the middle, then sorts the two arrays, after sorting, then the two arrays of the sequence are "merged" together, and the most important part of the merge sort is the "merging" process. The merging process requires additional space that is consistent with the number of two numbers that need to be merged, such as the required arrays: [3, 6, 8, 11] and [1, 3, 12, 15] (although logically marked as two arrays, but actually these elements are still in the original array, just through some index will It is divided into two arrays, the original array is [3, 6, 8, 11, 1, 3, 12, 15, we set three pointers lo, Mid, High, respectively 0,3,7 can achieve the logical sub-array division) then the extra array required is 4 + 4 = 8.
The process of merging can be summarized briefly as follows:
1) Copy the elements from the two sub-arrays into the new array copiedarray, in the example above, then Copiedarray = [3, 6, 8, 11, 1, 3, 12, 15];
2) set two pointers to the corresponding first element in the atomic array, assuming that the two pointers are named Leftidx and Rightidx, then Leftidx = 0 (corresponding to the first element in the Copiedarray [3]), Rightidx = 4 (corresponds to the CO The fifth element in the Piedarray [1]);
3) Compare the values of the array elements pointed to by LEFTIDX and Rightidx, select one of the smaller ones and assign their values to the corresponding position I in the original array, then do the self-increment 1 for the two indexes participating in the assignment, if the LEFTIDX or RIGTHIDX values have reached the end of the corresponding array. The remaining array elements are copied sequentially to the remaining position.
Here is a concrete example of merging:
First trip: auxiliary array [21, 28, 39 | 35, 38] (the array is split into left and right two sub-arrays to | separate) [21,, , , ] (first 21 vs. 35, left sub-array wins, LEFTIDX = 0, i = 0 ) Second trip: auxiliary array | [28] (the second time compared with 35, the left sub-array wins, LEFTIDX = 1, i = 1 ) Third trip: [21, 28, 39 | 3 5,[+] [39] (third time comparison with 35, right sub-array wins, RIGHTIDX = 0, i = 2 ) Fourth trip: [21, 28, 39 | [+] [+] [39] (38) (fourth time comparison, the right sub-array wins, RIGHTIDX = 1, i = 3 ) Fifth trip: [21, 28, 39 | [21, 28, 35, 38, 39] (the fifth time the right sub-array has been copied, without comparing LEFTIDX = 2, i = 4)
This is the process of merging, we can divide the entire array to be sorted by a finite number of splits (divided into two each) until the length of the 1 decimal group, the length of the 1 o'clock array is no longer sorted. After that, the reverse order (due to recursion) merges the arrays in sequence until the last N/2 of the length of the array, and the array is sorted after the merge is complete.
The additional space required for a merge sort is the largest of all sorts, and each merge needs to be the same as the sum of the two array lengths involved (in order to provide a secondary array). You can infer that the spatial complexity of the merge sort is 1 + 2 + 4 + ... + N = n * (n + 2)/4 (ignoring the parity of N), time complexity is more difficult to estimate, here the younger brother also forget how much (embarrassing).
Merge sort is an efficient sorting algorithm based on merging operations, which merges two sorted sequences into a sequence.
Implementation code:
/*** Merge Sort <br/> * <ul> * <li> apply space to size two sorted sequence of sum, the space used to hold the merged sequence </li> * <li> set two pointers, the most Initial position is two sorted sequence starting position </li> * <li> compare two pointers to the element, select a relatively small element into the merge space, and move the pointer to the next position </li> * <li> Repeat step 3 until a pointer reaches the end of the sequence </li> * <li> copies all remaining elements of another sequence directly to the end of the merge sequence </li> * </ul> *@paramnumbers*/ Public Static voidMergeSort (int[] numbers,intLeftintRight ) { intt = 1;//number of elements per group intSize = Right-left + 1; while(T <size) { ints = t;//number of elements per group in this cyclet = 2 *s; inti =Left ; while(i + (t-1) <size) {Merge (numbers, I, I+ (s-1), i + (t-1)); I+=T; } if(i + (s-1) <Right ) Merge (numbers, I, I+ (s-1), right); } } /*** Merge Algorithm implementation * *@paramData *@paramp *@paramQ *@paramR*/ Private Static voidMergeint[] Data,intPintQintr) {int[] B =New int[Data.length]; ints =p; intt = q + 1; intK =p; while(s <= q && t <=r) {if(Data[s] <=Data[t]) {B[k]=Data[s]; S++; } Else{B[k]=Data[t]; T++; } k++; } if(s = = q + 1) B[k+ +] = data[t++]; Elseb[k+ +] = data[s++]; for(inti = P; I <= R; i++) Data[i]=B[i]; }
Java algorithm-merge sort