Merge sort is another type of sorting method. The meaning of merging is to combine two or more ordered data sequences into a new ordered data sequence, therefore, it is also called the merge algorithm. The basic idea is to assume that array A has n elements, and array a is composed of N ordered subsequences. The length of each subsequence is 1, then merge them in two ways to obtain an n/2 ordered subsequences with a length of 2 or 1, and then merge them in two, it is worthwhile to obtain a sequence of ordered data whose length is N. This sorting method is called 2-way merge sorting.
For example, if array A has 7 data records, which are 49 38 65 97 76 13 27, then the operation process using the Merge Sorting Algorithm is 7:
Initial Value [49] [38] [65] [97] [76] [13] [27]
It is regarded as composed of seven subsequences with a length of 1.
[38 49] [65 97] [13 76] [27] after the first merge
It is regarded as composed of four subsequences with a length of 1 or 2.
[38 49 65 97] [13 27 76] after the second merge
It is considered to be composed of two subsequences with a length of 4 or 3.
[13 27 38 49 65 76 97] after the third merge
The core operation of the merge algorithm is to combine two adjacent sequential sequences in one-dimensional array into one ordered sequence. Merge algorithms can also be implemented using recursive algorithms. The form is simple, but the practicality is poor. The number of merging operations of the merge algorithm is a very important amount. When the array contains 3 to 4 elements, the number of merging operations is twice. When there are 5 to 8 elements, the number of merging operations is three. When there are 9 to 16 elements, the number of merging operations is four. According to this rule, when there are n subsequences, we can infer that the number of merging times is X (2> = N, the minimum X that meets this condition ).
The time complexity is O (nlogn). The auxiliary storage space required is O (n)
The merge algorithm is as follows:
# Include <stdio. h>
- # Include <stdlib. h>
- Typedef int rectype; // type of the element to be sorted
- Void Merge (rectype * r, int low, int M, int high)
- {
- // Combine two ordered sub-files R [low.. m) and R [M + 1 .. High] into an ordered sub-file R [low .. High]
- Int I = low, j = m + 1, P = 0; // set the Initial Value
- Rectype * R1; // R1 is a local vector
- R1 = (rectype *) malloc (high-low + 1) * sizeof (rectype ));
- If (! R1)
- {
- Return; // failed to apply for Space
- }
- While (I <= M & J <= high) // when the two sub-files are not empty, the small ones are output to R1 [p ].
- {
- R1 [p ++] = (R [I] <= R [J])? R [I ++]: R [J ++];
- }
- While (I <= m) // If the 1st sub-files are not empty, copy the remaining records to r1
- {
- R1 [p ++] = R [I ++];
- }
- While (j <= high) // If the 2nd sub-files are not empty, copy the remaining records to r1
- {
- R1 [p ++] = R [J ++];
- }
- For (P = 0, I = low; I <= high; P ++, I ++)
- {
- R [I] = R1 [p]; // after merging, copy the result back to R [low... high]
- }
- }
- Void mergesort (rectype R [], int low, int high)
- {
- // Sort R [low .. High] by means of the division and Control Law.
- Int mid;
- If (low
- {// The Interval Length is greater than 1
- Mid = (low + high)/2; // Decomposition
- Mergesort (R, low, mid); // recursively sorts R [low... mid]
- Mergesort (R, Mid + 1, high); // recursively sorts R [Mid + 1 .. High]
- Merge (R, low, mid, high); // combine two ordered zones into one ordered Zone
- }
- }
- Void main ()
- {
- Int A [7] = {, 27}; // sort the eight elements
- Int low = 0, high = 6; // initialize the values of low and high
- Printf ("before merge sort :");
- For (INT I = low; I <= high; I ++)
- {
- Printf ("% d", a [I]); // output test
- }
- Printf ("/N ");
- Mergesort (A, low, high );
- Printf ("after merge sort :");
- For (I = low; I <= high; I ++)
- {
- Printf ("% d", a [I]); // output test
- }
- Printf ("/N ");
- }