Thoughts on Merge Sorting and its spatial complexity
Merge Sorting adopts the divide and conquer method.
For Merge Sorting:
If a temporary array is declared for each recursive call to Merge, there may be logN temporary arrays in active State at any time, which is fatal to small memory machines. On the other hand, if Merge dynamically allocates and releases the minimum temporary space, there will be a lot of time occupied by malloc. Because Merge is located in the last row of MSort, you can create this temporary array in MergeSort. Therefore, you only need a temporary array activity at any time, and you can use any part of the temporary array. We will use the same part as the input array. In this case, the space occupied by the algorithm is N, and N is the number of elements in the array to be sorted.
/* Tmp_array []: Secondary array. Left_pos: cursor of the left half of the array left_end: Right Border of the Left array */void Merge (int array [], int tmp_array [], int left_pos, int right_pos, int right_end) {int I, left_end, num_elements, tmp_pos; left_end = right_pos-1; tmp_pos = left_pos; num_elements = right_end-left_pos + 1; while (left_pos <= left_end & right_pos <= right_end) if (array [left_pos] <= array [right_pos]) tmp_array [tmp_pos ++] = array [left_pos ++]; elsetmp_array [tmp_po S ++] = array [right_pos ++]; while (left_pos <= left_end) tmp_array [tmp_pos ++] = array [left_pos ++]; while (right_pos <= right_end) tmp_array [tmp_pos ++] = array [right_pos ++]; for (I = 0; I <num_elements; I ++, right_end --) array [right_end] = tmp_array [right_end];} void MSort (int array [], int tmp_array [], int left, int right) {int center; if (left <right) {center = (left + right)/2; MSort (array, tmp_array, Left, center); MSort (array, tmp_array, center + 1, right); Merge (array, tmp_array, left, center + 1, right );}} void MergeSort (int array [], int n) {int * tmp_array; // The text section above shows why a temporary array tmp_array = (int *) is created in MergeSort *) malloc (n * sizeof (int); if (tmp_array! = NULL) {MSort (array, tmp_array, 0, n-1); free (tmp_array);} elsecout <"malloc failed" <endl ;}
Although the Merge Sorting time is O (NlogN), it is difficult to use for primary storage sorting, because the merge of two sort tables requires linear additional memory, the whole algorithm also needs to copy the data to a temporary array and then copy it back, which seriously affects the sorting speed.
For important internal sorting applications, quick sorting is often selected. The merge method is the cornerstone of most external sorting algorithms..
------------------------------------------------------ Split ------------------------------------------------------------------
The following article describes how to implement Merge Sorting under the condition of space complexity O (1 ).
The original article is as follows:
First, for a sub-sequence of L1, L2, the length is m, n. The space of min (m, n) can be used to assist in merging and arranging.
And only affects the order of the extra space. For more information, see the algorithm book in sara basse.
Second: For a sorted L1, L2, the total length vacation is set to u. For convenience analysis, assume that the L1 length = L2 length = u/2, and split it into sqrt (u) of
Block, each with sqrt (u) number, then merge the last part of L1 and the last part of L2, the last part of L2
Block stores the maximum data of all data, and uses the last L2 block as the additional space for merging.
Third: for the remaining blocks (except the last part of L2, the block stores the largest element), sort by the minimum element of the block, and merge adjacent blocks in sequence.
Fourth: sort the last part. Because it is used as an extra space and participates in the sorting of adjacent parts, its order cannot be guaranteed, so it will be sorted again at the end.
The correctness of this method can be simply explained in this way.
Because the last part is cut from L1 or L2.
Then block I, block I + 1 and block I + 2 (if sorted by the smallest element)
Assume that block I is from L1 and block I + 1 is from L2. (I, I + 1, I + 2, is the cut-down number. For example, in the example, 1st is the first block of 1 4 6 15 is the L1, 2nd blocks, 2 3 4 16, 1st L2 blocks)
Where can I + 2 be from? Either the original L1 block is behind block I or the original L2 block is behind block I + 1. That is, block I + 1 must be at least greater than the number of N (N is the number of blocks ), in other words, the smallest block I after I and I + 1 are smaller than I + 1. This is the difficulty of this algorithm.
Therefore, the maximum elements of the I block after I and I + 1 are smaller than the minimum elements of the I + 1 block. Therefore, the correctness can be ensured.
Draw a graph for example:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141221/20141221091150317.gif" alt = "\">
Figure 1 merge the largest blocks of L1 and L2, and use a swap space with four elements to obtain the maximum blocks of L1 & L2 22 23 24 25
Figure 2 merge the elements in the order of minimum block elements and use the swap space
Figure 3 the process of merging swap spaces. Note that the number of swap spaces after merging is still 22, 23, 24, and 25, but the order has changed. Therefore, you need to sort the swap space after merging.
4 5 6 15 and 16 17 20 21 are no longer shown in the figure.