Improvement of Merge Sorting Algorithm

Source: Internet
Author: User

Merge and sort: merge two adjacent sequence in a group of arrays into an ordered sequence.

Core operation: Assume that there are n sequences, and then merge them to obtain [n/2] subsequences with a length of 2 or 1, and then merge them in two ,...... Until an ordered sequence with the length of n is obtained, this sorting method is two-way merge sorting.

Let's look at the following two-way merge algorithm.

int MSort(MergeType S, MergeType *pT, int nStart, int nEnd){int nMidPos = 0;if ( !S.elem || nStart > nEnd){return -1;}if (nStart == nEnd){pT->elem[nStart] = S.elem[nStart];return 0;}nMidPos = (nStart + nEnd) / 2;MSort(S, pT, nStart, nMidPos);MSort(S, pT, nMidPos + 1, nEnd);Merge(S, pT, nStart, nMidPos, nEnd);return 0;}

The above step is to split the data in half until there is only one data and then merge it. This is a recursive operation. Let's take a look at how to merge the data:

/* S: original data list, pT: sorted data list. For other data and function definitions, see bubble Algorithm Improvement */

Int Merge (MergeType S, MergeType * pT, int nStart, int nPos, int nEnd) {int nSi = nStart, nSj = nPos + 1; int nTi = nStart; (; nSi <= nPos & nSj <= nEnd;) {// The error pT-> elem [nTi ++] = (S. elem [nSi] <= S. elem [nSj])? S. elem [nSi ++]: S. elem [nSj ++];} // the remaining data while (nSi <= nPos) {pT-> elem [nTi ++] = S. elem [nSi ++];} while (nSj <= nEnd) {pT-> elem [nTi ++] = S. elem [nSj ++];} return 0 ;}
When merging, two arrays (only one data) are first merged into an array, and of course two different arrays (n> = 2) are merged into an array. However, there are still some situations:

Assume that an array is (, 24) and the other is (). At this time, (, 24) will be filled in the array T, but the other array) the array T has not been inserted, and the next two while loops only need to insert the remaining values to the end of the array T. At this time, no comparison is required. It must be inserted at the end of the array. The for loop comparison already knows who is the size, in addition, the remaining arrays are sorted.

On the surface, the above algorithm is correct. In fact, there is a problem during the merge process. It has been explained in the comments. Please think about how to correct the error? The error is not always there !!

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.