Two-way Merge Sorting (also called Merge Sorting) and two-way merge

Source: Internet
Author: User

Two-way Merge Sorting (also called Merge Sorting) and two-way merge

The following figure shows the two-way merge process.


The core code of the two-way merge is the merge () function.

It merges two split arrays in an ordered manner.

In array,

From p to q is an array, and from q to r is another array.

Then, how can we combine the two arrays in order to form A new array?

Steps:

Step 1: Open up an array L and store the elements between p and q (that is, the left array to be merged)

The second part: Open up an array R and store the elements between q and r (that is, the right array to be merged ).

Step 3: add an infinite number at the end of the two arrays (which can be expressed by 0x7FFF). Therefore, the open array space must be 1 character more.

Step 4: Compare the numbers in L and R one by one, and put the smaller ones in array A first (store them from array A [0] and overwrite the original number in sequence ), then the small array pointer moves backward, pointing to the next bit and then comparing it with another Array



// The first parameter is the array to be sorted, 2nd parameters are the subscript of the starting element of the first split array // 3rd parameters are the subscript of the last element of the first split array // 4th parameters are the last 1 of the array element subscript void Merge (int *, int p, int q, int r) {int n1, n2, I, j, k, g; n1 = q-p + 1; n2 = r-q; int * L, * R; L = (int *) malloc (sizeof (int) * (n1 + 1); R = (int *) malloc (sizeof (int) * (n2 + 1); L [n1] = 0x7fff; // The number of the last two arrays opened up is set to the maximum value R [n2] = 0x7fff; g = 0; for (I = p; I <= q; I ++) {L [g] = A [I]; g ++;} g = 0; for (I = q + 1; I <= r; I ++) {R [g] = A [I]; g ++ ;} // compare the Left and Right arrays one by one, and write smaller values into the original array j = k = 0; for (I = p; I <= r; I ++) {if (L [j] <R [k]) {A [I] = L [j]; j ++ ;} else {A [I] = R [k]; k ++ ;}}}

Complete code:

# Include <iostream> using namespace std; // The first parameter is the array to be sorted, 2nd parameters are the subscript of the starting element of the first split array // 3rd parameters are the subscript of the last element of the first split array // 4th parameters are the last 1 of the array element subscript void Merge (int *, int p, int q, int r) {int n1, n2, I, j, k, g; n1 = q-p + 1; n2 = r-q; int * L, * R; L = (int *) malloc (sizeof (int) * (n1 + 1); R = (int *) malloc (sizeof (int) * (n2 + 1); L [n1] = 0x7fff; // The number of the last two arrays opened up is set to the maximum value R [n2] = 0x7fff; g = 0; for (I = p; I <= q; I ++) {L [g] = A [I]; g ++;} g = 0; for (I = q + 1; I <= r; I ++) {R [g] = A [I]; g ++ ;} // compare the Left and Right arrays one by one, and write smaller values into the original array j = k = 0; for (I = p; I <= r; I ++) {if (L [j] <R [k]) {A [I] = L [j]; j ++ ;} else {A [I] = R [k]; k ++ ;}} void MergeSort (int * A, int p, int r) {int q; if (p <r) // when the first element is less than the last element, the recursion is continued until only one element is left (the parameter p = r) {q = (p + r)/2; MergeSort (A, p, q); MergeSort (A, q + 1, r); Merge (A, p, q, r) ;}} void main () {int A [5] = {5, 3, 11}; MergeSort (A,); for (int I = 0; I <5; I ++) cout <A [I] <endl; system ("pause ");}





Describe the two-way Merge Sorting and analyze the complexity of the algorithm.

Two-way merge is to merge two ordered sequences into one ordered sequence.

Sorting is an unordered sequence, which is then divided into two ordered sequences.
Here we use a recursive idea.
That is, the algorithm is cut into two segments, and an ordered sequence can be obtained by applying the algorithm to the first and second segments. This has two ordered sequences, then we use this algorithm to get an ordered sequence.
The recursive endpoint is that when there is only one element in a segment, it is obviously an ordered sequence and can return

The specific code is:
Void Merge (int r [], int r1 [], int s, int m, int t) // two-way Merge
{
Int I = s, j = m + 1, k = s;
While (I <= m & j <= t)
{
If (r [I] <= r [j]) r1 [k ++] = r [I ++];
Else r1 [k ++] = r [j ++];
}
If (I <= m)
{
While (I <= m)
R1 [k ++] = r [I ++];
}
Else
{
While (j <= t)
R1 [k ++] = r [j ++];
}
}

Void MergeSort (int r [], int r1 [], int s, int t) // recursive call
{
If (s = t) r1 [s] = r [s];
Else {
M = (s + t)/2;
MergeSort (r, r1, s, m );
MergeSort (r, r1, m + 1, t );
Merge (r1, r, s, m, t );
}
}

As for its time complexity, I strictly analyzed it as O (nlog2n). I have tested it. It performs no less performance than fast sorting and heap sorting in big data sorting, it is not related to the sequence of initial data. It is a stable sorting algorithm.
The disadvantage is its spatial complexity, which reaches O (n)

In addition, it has non-recursive algorithms with the same idea. I will not talk about them much. If you need them, you can Hi me.

Explain the two-way Merge Sorting method, C Language

# Include <stdio. h>
# Include <malloc. h>
Void merge (int * a, int beg, int mid, int end) // merge subsequences
{
Int I = beg, j = mid + 1, cnt = 0;
Int * a1;
A1 = (int *) malloc (end-beg + 1) * sizeof (int ));

While (I <= mid & j <= end)
{
A1 [cnt ++] = a [I] <= a [j]? A [I ++]: a [j ++];
}
While (I <= mid)
{
A1 [cnt ++] = a [I ++];
}
While (j <= end)
{
A1 [cnt ++] = a [j ++];
}
For (cnt = 0, I = beg; I <= end; cnt ++, I ++)
{
A [I] = a1 [cnt];
}

}
Void merge_sort (int * a, int beg, int end) // sort two-way merge
{
Int mid = 0;
If (beg <end)
{
Mid = (beg + end)/2; // split the Left and Right Parts
Merge_sort (a, beg, mid); // sort the left half
Merge_sort (a, mid + 1, end); // sort the right half
Merge (a, beg, mid, end); // merge the Left and Right Parts
}
}

Int main (void)
{
Int a [10] = };

Int I = 0, j = 0, k = 0;
For (I = 0; I <10; I ++)
{
Printf ("% 4d", a [I]);
}
Putchar ('\ n ');
Merge_sort (a, 0, 9 );
For (I = 0; I <10; I ++)
{
Printf ("% 4d", a [I]);
}
Putchar ('\ n ');

}

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.