Sort Algorithm series-merge sort

Source: Internet
Author: User

Merge Sorting

Written By lpstudy, A man ofstriving for life. Please mark the deviation if you copy it.

1. Sort description

Merge Sorting is an important idea of recursive sorting. It divides a big problem into two subproblems, and then solves the subproblems of the two smaller points respectively. After the results are merged, they are the solutions of the original big problem. Because this is recursive, it was a very big problem to be split by 1.1 points, and finally split into the simplest situation, which can be solved directly, then merge a few simple cases one by one, and the final result is the result. It is a bit complicated, but the code implementation uses recursion, and it looks very neat and clear.

Void MergeSort (entire A) {if (meets the condition) // This is actually A recursive condition, or it can be viewed in turn as A recursive end condition {MergeSort (the first half of ); // subproblem, the first half of A MergeSort (the last half of A); // subproblem, the last half of A Merge (the first half of A and the last half of ); // merge the solutions of two subproblems }}

2. Storage Structure

This sorting algorithm can be directly stored in arrays. The following is an example of the sorting process. There are 8 Elements in total.

L first, the problem of eight elements is divided into two four elements. That is, 9 2 83 and 7 1 0 6

L then, for the first four problems, namely 9 2 8 3, the problem is further decomposed into two elements, namely 92 and 8 3.

L for the previous issue 92, we continue to break down the problem into two subproblems 9 and 2.

L again for the previous little boy problem 9, it has only one element, so it must be ordered and no sorting is needed.

L start the merging process. Merge the sub-problems 9 and 2 into; merge the sub-problems 8 and 3.

L continue to merge and into, 9, 8. The other half is also merged

L merge two subproblems to obtain the final result 0, 1, 2, 3, 6, 8, 9.

L shows the result.

Index

0

1

2

3

4

5

6

7

Element value

9

2

8

3

7

1

0

6

1st

2

9

3

8

1

7

0

6

2nd

2

3

8

9

0

1

6

7

3rd

0

1

2

3

6

7

8

9

3. algorithm code

There are two main parts: one is the overall recursive framework, and the other is the merge problem for two subproblems.

3.1 overall framework

// Pay attention to the ending condition of recursion, that is, when there is only one element, recursion ends.

Typedef int ElemType; void MergeSort (ElemTypeA [], int low, int high) {if (low
 
  

3.2 merge functions

// Merge two subproblems. Void Merge (ElemType A [], int low, int mid, int high) {// merge two ordered tables A [low-> mid] And A [mid + 1-> high. // You need to use the third table to help Merge data. For the sake of simplicity, I define a local static array to act as the temporary data const int MAX_SIZE = 9999; static ElemType H [MAX_SIZE]; // secondary MERGE table int m = mid ++; int k = 0; // index of the merged H table while (low <= m & mid <= high) {if (A [low] = 0; -- I, -- high) A [high] = H [I]; // copy the merged set back}

3.3 time complexity

From the algorithm process, we can see that each sub-problem is merged into an O (n) level, and each sub-problem is T (n/2), so the time is T (n) = 2 * T (n/2) + O (n ). This time complexity is O (nlgn), and it is a stable sorting.

The introduction to algorithms proves that T (n) = a * T (n/B) + f (n)

If f (n) <, T (n) = O ()

If f (n) =, T (n) = O (* lgn)

If f (n)>, T (n) = O () where when n approaches infinity, a * f (n/B)/f (n) <1

For example:

T (n) = 2 T (n/2) + O (1) Where B = a = 2, so it is 1st cases, T (n) = O (n)

T (n) = 2 T (n/2) + O (n) Where B = a = 2, so it is 2nd cases, T (n) = O (n * lgn)

T (n) = 2 T (n/2) + O (n * n) Where B = a = 2, so it is 3rd cases, T (n) = O (n * n)

4. Execute Code and 4.1 complete Execution Code

# Include
   
    
Using namespacestd; typedef int ElemType; // merge two subproblems. Void Merge (ElemType A [], int low, int mid, int high) {// merge two ordered tables A [low-> mid] And A [mid + 1-> high. // You need to use the third table to help Merge data. For the sake of simplicity, I define a local static array to act as the temporary data const int MAX_SIZE = 9999; static ElemType H [MAX_SIZE]; // secondary MERGE table int m = mid ++; int k = 0; // index of the merged H table while (low <= m & mid <= high) {if (A [low] = 0; -- I, -- high) A [high] = H [I]; // copy the merged set back.} void MergeSort (ElemTypeA [], int low, int high) {if (low
    
     

4.2 run

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.