Sorting algorithm's merge sort (mergesort) parsing

Source: Internet
Author: User

I. Advantages and disadvantages of merge sorting (pros and cons)

It takes a lot of thought to understand it, and there's always a reason:

    • The efficiency of merge sorting reaches its peak: time complexity is O (NLOGN), which is the highest level that can be achieved by comparison-based sorting algorithm
    • Merge sort is a stable algorithm (that is, when the same size elements in the sorting process can maintain the order before sorting, the 3212 ascending sort result is 1223, the order of the two 2 before and after sorting is unchanged), which is critical in some scenarios
    • Merge sort is the most commonly used external sorting method (when the records to be sorted are placed on external memory, and the memory does not fit all the data, the merge sort still applies, but the merge sort also applies to the internal sort ...). )

Disadvantages:

    • Merge sort requires O (n) of the auxiliary space, and the efficiency of the same fast row and the heap row respectively need O (Logn) and O (1) of the auxiliary space, in the same algorithm, the spatial complexity of the merging of the order is slightly higher

P.S. This article only discusses the most primitive "two-way merger", the same as the multi-path

Two. Internal principles

The first thing to know is that the idea of merging sorts is: divide and conquer, just like the idea of fast sequencing

Algorithmic thinking: Order and order

The process of "divide" and "close" in merge sort is combined, that is, every trip is doing "points" and "Hopewell" work, not first "score" and then "close" ("points" is very simple, not just two points until you can not be divided Bai, the amount, so think on the wrong, divided the clash not up, remember "points" and "close" are combined)

The process of interpreting the merge sort with a picture is sufficient:

Note: P1 and P2 Compare, the larger (small) loaded into p, and then P1 or P2 right shift (installed who will move who), and finally the right shift p

For example, to do an array of a[n] in ascending order, then the specific process is as follows:

    1. Apply for two lengths of auxiliary space N/2, put an array of a, the first half into L, and half into R
    2. Make a round comparison as described (P moves from a to C, end of trip)

Now think about what we've done in a sort of order.

    1. A partial order (half < half, right?) )
    2. In addition to that, we naturally split the ordered sequence into the same order, ready for recursion.

Not clear enough yet? Then there are a few more words:

    1. The process of illustration explains why a secondary space of length n is required
    2. As long as L and R are internally ordered (in ascending or descending order), then only one more merge is required, and the sorting is done (think about it, right?). )
    3. The sort part is the merge section (remember the sentence mentioned above?) "Divide" and "close" the process is combined together, must not separate think, otherwise you will find not close up ... )

Three. Implementation Details

If the above explanation is not clear enough, let's take an example, step-by-step analysis:

Assuming that the sequence to be sorted is a[] = {3, 2, 1, 4}, then the process is as follows:

P.S. What if the sequence to be sorted is an odd number? Is that a problem? Nothing but the first half of the demolition is less than the last half, single-pass loop control is done by the P-pointer, there is no one P1 with the P2 can match the problem

Four. Summary

Merge sort is used for scenarios where external ordering is required, in addition, when the internal ordering needs to ensure stability is also used in the merge sort (does not require stability of the internal ordering is generally used fast or heap sorting, the former in order to sort the sequence of the basic order of low efficiency, the latter heap maintenance is a problem)

Sorting algorithm's merge sort (mergesort) parsing

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.