Merge Sorting and merge sorting algorithms

Source: Internet
Author: User

Merge Sorting and merge sorting algorithms

The core idea of merging and sorting is the principle of separation and Governance: decomposing, solving, and merging problems. The problem factorization divides n elements into subsequences of n/2 elements. The problem is to use the merge sort method to recursively sort the two subsequences; problem merging is to merge two sorted subsequences into a new sequence to obtain the sorting result. We can see that merging of sorted sequences is the key to the problem.

1. Merge sorted sequences: the process is represented by graphs!

# Define INFTY 2147483647 void Merge (int a [], int low, int mid, int high) {int n1 = mid-low + 1, n2 = high-mid; int * L = new int [n1 + 1]; // you can add one more sentinel element to simplify the code: int * R = new int [n2 + 1]. // Add one more sentinel element to simplify the code for (int I = 0; I <n1; ++ I) {L [I] = a [low + I];} for (int j = 0; j <n2; ++ j) {R [j] = a [mid + j + 1];} L [n1] = INFTY; // The value of the Sentinel element is positive infinity R [n2] = INFTY; for (int I = 0, j = 0, k = low; k <= high; ++ k) {// used to merge the array if (L [I] <= R [j]) {a [k] = L [I]; ++ I ;} else {a [k] = R [j]; ++ j ;}} delete L; delete R ;}

The first element 1 of the right sequence R is compared with the first element of the Left sequence L. Because 1 is smaller than 2, 1 of the R is placed in the merged array, next, compare the elements with the value of 2 in L with the element with the value of 2 in R. The result is equal. Place 2 in the merged array, and so on.

The Code is as follows:


2. Merge Sorting:

Code:

Void mergeSort (int a [], int low, int high) {if (low 





Merge Sorting

First, consider a simple question: how to merge two ordered queues into one ordered queue (and output) in a linear time )?

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9

As shown in the above example, the AB sequence is already ordered. When data is given in an orderly manner, we will find a lot of magical things. For example, the first number we will output must come from the number at the beginning of each of the two sequences. If the two numbers are both 1, we can retrieve one (for example, the one in queue A) and output:

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9
Output: 1

Note: We have taken out a number and deleted it from the original series. The delete operation is implemented by moving the first pointer of the team, otherwise the complexity is high.
Now, the number of headers in queue A is 3, and the number of headers in queue B is still 1. At this point, we can compare the values 3 and 1 and output a small number:

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9
Output: 1 1

The following steps:

A queue: 1 3 5 7 9 A queue: 1 3 5 7 9 A queue: 1 3 5 7 9 A queue: 1 3 5 7 9
B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ......
Output: 1 1 2 output: 1 1 2 3 output: 1 1 2 3 5 output: 1 1 2 3 5 7

I hope you understand how this is done. This is obviously correct, and the complexity is obviously linear.

Merge Sort will use the Merge operation mentioned above. A series is given, and the series are sorted from small to large in the time of O (nlogn) using the merge operation. Merge Sorting uses the idea of Divide and Conquer. First, we divide the given series equally into the left and right sections, then sort the two series separately, and finally use the merge algorithm to sort the two segments (sorted) merge a series into a series. Someone will ask "What sort is used for sorting the numbers of the left and right columns separately? The answer is: sort by merging. That is to say, we recursively divide each series into two sections for the above operation. You don't need to worry about how it actually works. Our program code will call this process recursively until the series cannot be divided (only one number.
When I first looked at this algorithm, some people mistakenly thought that the time complexity was quite high. The following figure provides a non-recursive view of the actual operation process of Merge Sorting for your reference. We can use this graph to prove that the time complexity of the Merge Sorting Algorithm is O (nlogn ).

[3] [1] [4] [1] [5] [9] [2] [7]
\/\/\/\/
[1 3] [1 4] [5 9] [2 7]
\/\/
[1 1 3 4] [2 5 7 9]
\/
[1 1 2 3 4 5 7 9]

Each "\/" in indicates the linear time merge operation described above. 4 rows are used to illustrate the Merge Sorting. If there are n numbers, it indicates that O (logn) rows are required. The total complexity of merge operations for each row is O (n), so the total complexity of logn rows is O (nlogn ). This is equivalent to analyzing the complexity of merging and sorting by using the recursive tree method. Assume that the complexity of Merge Sorting is T (n), and T (n) is composed of two T... the remaining full text>

Merge Sorting

First, consider a simple question: how to merge two ordered queues into one ordered queue (and output) in a linear time )?

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9

As shown in the above example, the AB sequence is already ordered. When data is given in an orderly manner, we will find a lot of magical things. For example, the first number we will output must come from the number at the beginning of each of the two sequences. If the two numbers are both 1, we can retrieve one (for example, the one in queue A) and output:

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9
Output: 1

Note: We have taken out a number and deleted it from the original series. The delete operation is implemented by moving the first pointer of the team, otherwise the complexity is high.
Now, the number of headers in queue A is 3, and the number of headers in queue B is still 1. At this point, we can compare the values 3 and 1 and output a small number:

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9
Output: 1 1

The following steps:

A queue: 1 3 5 7 9 A queue: 1 3 5 7 9 A queue: 1 3 5 7 9 A queue: 1 3 5 7 9
B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ......
Output: 1 1 2 output: 1 1 2 3 output: 1 1 2 3 5 output: 1 1 2 3 5 7

I hope you understand how this is done. This is obviously correct, and the complexity is obviously linear.

Merge Sort will use the Merge operation mentioned above. A series is given, and the series are sorted from small to large in the time of O (nlogn) using the merge operation. Merge Sorting uses the idea of Divide and Conquer. First, we divide the given series equally into the left and right sections, then sort the two series separately, and finally use the merge algorithm to sort the two segments (sorted) merge a series into a series. Someone will ask "What sort is used for sorting the numbers of the left and right columns separately? The answer is: sort by merging. That is to say, we recursively divide each series into two sections for the above operation. You don't need to worry about how it actually works. Our program code will call this process recursively until the series cannot be divided (only one number.
When I first looked at this algorithm, some people mistakenly thought that the time complexity was quite high. The following figure provides a non-recursive view of the actual operation process of Merge Sorting for your reference. We can use this graph to prove that the time complexity of the Merge Sorting Algorithm is O (nlogn ).

[3] [1] [4] [1] [5] [9] [2] [7]
\/\/\/\/
[1 3] [1 4] [5 9] [2 7]
\/\/
[1 1 3 4] [2 5 7 9]
\/
[1 1 2 3 4 5 7 9]

Each "\/" in indicates the linear time merge operation described above. 4 rows are used to illustrate the Merge Sorting. If there are n numbers, it indicates that O (logn) rows are required. The total complexity of merge operations for each row is O (n), so the total complexity of logn rows is O (nlogn ). This is equivalent to analyzing the complexity of merging and sorting by using the recursive tree method. Assume that the complexity of Merge Sorting is T (n), and T (n) is composed of two T... the remaining full text>

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.