Merge Sorting-detailed explanation

Source: Internet
Author: User

Merge sort uses the "merge" Technology for sorting. Merging refers to merging several sorted sub-files into an ordered file.

1. Basic algorithm ideas

Set two ordered sub-files (equivalent to the input heap) to the adjacent positions in the same vector: R [low .. m], R [M + 1 .. high], first merge them into a local temporary storage vector r1 (equivalent to the output heap), and then copy R1 back to R [low .. high.

(1) merging process
During the merge process, set the I, j, and P pointers, and their initial values point to the starting positions of the three record areas respectively. When merging, compare the keywords of R [I] and R [J] in sequence, and copy the records with smaller keywords to R1 [p, then add the pointer I or J of the copied record to 1 and the pointer P pointing to the copy position to 1.
Repeat this process until one of the two input sub-files has been completely copied (it may be called null ), copy the remaining records of another non-empty sub-file to R1.

(2) dynamically apply for r1
During implementation, R1 dynamically applies because the application space may be large, so it must be added to the application space for successful processing.

2. Merge Algorithms
Void Merge (seqlist 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
// Sub-file R [low .. High]
Int I = low, j = m + 1, P = 0; // set the Initial Value
Rectype * R1; // R1 is a local vector. If P is defined as a pointer of this type, the speed is faster.
R1 = (reetype *) malloc (high-low + 1) * sizeof (rectype ));
If (! R1) // failed to apply for Space
Error ("insufficient memory available! ");
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]. Key <= R [J]. Key )? 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]
} // Merge

Merge Sorting can be implemented in two ways: bottom-up and top-down.

1. bottom-up method
(1) Basic Idea of bottom-up
The basic idea of bottom-up sorting is to sort the files to be sorted by merging 1st rows [1 .. n] is considered as N ordered sub-files with a length of 1. These Sub-files are merged into two pairs. If n is an even number, an ordered sub-file with a length of 2 is obtained; if n is an odd number, the wheel of the last sub-file is empty (No

Join ). Therefore, after this merge, the length of the first ordered sub-file is 2, but the most

The length of the next sub-file is still 1; the 2nd-byte merge is the result of merging the 1st-byte merge.

The sub-files in the order are merged into each other until an ordered file with the length of N is obtained.
The preceding merge operation combines two ordered sub-files into an ordered sub-file, which is called "two-way Merge Sorting ". Similarly, there are K (k> 2) Merge Sorting.

(2) the whole process of two-way Merge Sorting (omitted)
 
(3) Merge Algorithms
Analysis:
In a merge, if the length of each sub-file is set to length (the length of the last sub-file may be smaller than length), the pre-merge R [1 .. n] contains a total of ordered sub-Files: R

[1 .. length], R [Length + 1 .. 2 length],…, .
Note:
When the merge operation is called to merge adjacent sub-files, the number of sub-files may be odd, and the length of the last sub-file may be smaller than the length:
① If the number of sub-files is odd, the last sub-file does not need to be merged with other sub-files (that is, this round is empty );
② If the number of subfiles is an even number, note that the upper limit of the last subfile In the last pair is N.

The specific algorithm is as follows:
Void mergepass (seqlist R, int length)
{// Sort R [1 .. n] by merging
Int I;
For (I = 1; I + 2 * length-1 <= N; I = I + 2 * length)
Merge (R, I, I + Length-1, I + 2 * length-1 );
// Merge two adjacent sub-files with length
If (I + Length-1 <n) // There are two sub-files, the last of which is smaller than the length
Merge (R, I, I + Length-1, n); // merge the last two subfiles
// Note: If I ≤ n and I + Length-1 ≥ n, the rotation of the remaining sub-file is empty and does not need to be merged.
} // Mergepass

(4) two-way Merge Sorting Algorithm
Void mergesort (seqlist R)
{// Uses the bottom-up method to sort the R [1. N] by two-way Merging
Int length;
For (1 ength = 1; length <n; length * = 2) // merge

Mergepass (R, length); // terminate when the length of an ordered segment is greater than or equal to n.
}

Note:
Although the bottom-up Merge Sorting Algorithm is more efficient, it is less readable.

2. Top-Down Method
Use the division and Control Method for top-down algorithm design, the form is more concise.

(1) three steps of the divide and conquer Law
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 ).

(2) specific algorithms
Void mergesortdc (seqlist R, int low, int high)
{// Sort the R [low .. High] by merging and dividing
Int mid;
If (low Mid = (low + high)/2; // Decomposition
Mergesortdc (R, low, mid); // recursively sorts R [low... mid]
Mergesortdc (R, Mid + 1, high); // recursively sorts R [Mid + 1 .. High]
Merge (R, low, mid, high); // combine two ordered zones into one ordered Zone
}
} // Mergesortdc

(3) Execution Process of the algorithm mergesortdc (omitted)
Shows the implementation process of the algorithm mergesortdc.
Ii. 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.
Note:
If a single-chain table is used as the storage structure, it is easy to give the Merge Sorting in place.

Merge sort uses the "merge" Technology for sorting. Merging refers to merging several sorted sub-files into an ordered file.

1. Basic algorithm ideas

Set two ordered sub-files (equivalent to the input heap) to the adjacent positions in the same vector: R [low .. m], R [M + 1 .. high], first merge them into a local temporary storage vector r1 (equivalent to the output heap), and then copy R1 back to R [low .. high.

(1) merging process
During the merge process, set the I, j, and P pointers, and their initial values point to the starting positions of the three record areas respectively. When merging, compare the keywords of R [I] and R [J] in sequence, and copy the records with smaller keywords to R1 [p, then add the pointer I or J of the copied record to 1 and the pointer P pointing to the copy position to 1.
Repeat this process until one of the two input sub-files has been completely copied (it may be called null ), copy the remaining records of another non-empty sub-file to R1.

(2) dynamically apply for r1
During implementation, R1 dynamically applies because the application space may be large, so it must be added to the application space for successful processing.

2. Merge Algorithms
Void Merge (seqlist 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
// Sub-file R [low .. High]
Int I = low, j = m + 1, P = 0; // set the Initial Value
Rectype * R1; // R1 is a local vector. If P is defined as a pointer of this type, the speed is faster.
R1 = (reetype *) malloc (high-low + 1) * sizeof (rectype ));
If (! R1) // failed to apply for Space
Error ("insufficient memory available! ");
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]. Key <= R [J]. Key )? 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]
} // Merge

Merge Sorting can be implemented in two ways: bottom-up and top-down.

1. bottom-up method
(1) Basic Idea of bottom-up
The basic idea of bottom-up sorting is to sort the files to be sorted by merging 1st rows [1 .. n] is considered as N ordered sub-files with a length of 1. These Sub-files are merged into two pairs. If n is an even number, an ordered sub-file with a length of 2 is obtained; if n is an odd number, the wheel of the last sub-file is empty (No

Join ). Therefore, after this merge, the length of the first ordered sub-file is 2, but the most

The length of the next sub-file is still 1; the 2nd-byte merge is the result of merging the 1st-byte merge.

The sub-files in the order are merged into each other until an ordered file with the length of N is obtained.
The preceding merge operation combines two ordered sub-files into an ordered sub-file, which is called "two-way Merge Sorting ". Similarly, there are K (k> 2) Merge Sorting.

(2) the whole process of two-way Merge Sorting (omitted)
 
(3) Merge Algorithms
Analysis:
In a merge, if the length of each sub-file is set to length (the length of the last sub-file may be smaller than length), the pre-merge R [1 .. n] contains a total of ordered sub-Files: R

[1 .. length], R [Length + 1 .. 2 length],…, .
Note:
When the merge operation is called to merge adjacent sub-files, the number of sub-files may be odd, and the length of the last sub-file may be smaller than the length:
① If the number of sub-files is odd, the last sub-file does not need to be merged with other sub-files (that is, this round is empty );
② If the number of subfiles is an even number, note that the upper limit of the last subfile In the last pair is N.

The specific algorithm is as follows:
Void mergepass (seqlist R, int length)
{// Sort R [1 .. n] by merging
Int I;
For (I = 1; I + 2 * length-1 <= N; I = I + 2 * length)
Merge (R, I, I + Length-1, I + 2 * length-1 );
// Merge two adjacent sub-files with length
If (I + Length-1 <n) // There are two sub-files, the last of which is smaller than the length
Merge (R, I, I + Length-1, n); // merge the last two subfiles
// Note: If I ≤ n and I + Length-1 ≥ n, the rotation of the remaining sub-file is empty and does not need to be merged.
} // Mergepass

(4) two-way Merge Sorting Algorithm
Void mergesort (seqlist R)
{// Uses the bottom-up method to sort the R [1. N] by two-way Merging
Int length;
For (1 ength = 1; length <n; length * = 2) // merge

Mergepass (R, length); // terminate when the length of an ordered segment is greater than or equal to n.
}

Note:
Although the bottom-up Merge Sorting Algorithm is more efficient, it is less readable.

2. Top-Down Method
Use the division and Control Method for top-down algorithm design, the form is more concise.

(1) three steps of the divide and conquer Law
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 ).

(2) specific algorithms
Void mergesortdc (seqlist R, int low, int high)
{// Sort the R [low .. High] by merging and dividing
Int mid;
If (low Mid = (low + high)/2; // Decomposition
Mergesortdc (R, low, mid); // recursively sorts R [low... mid]
Mergesortdc (R, Mid + 1, high); // recursively sorts R [Mid + 1 .. High]
Merge (R, low, mid, high); // combine two ordered zones into one ordered Zone
}
} // Mergesortdc

(3) Execution Process of the algorithm mergesortdc (omitted)
Shows the implementation process of the algorithm mergesortdc.
Ii. 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.
Note:
If a single-chain table is used as the storage structure, it is easy to give the Merge Sorting in place.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.