Merge sort and reverse sort Algorithms

Source: Internet
Author: User

Qsort is used for sorting. Other sorting algorithms are not very useful, but some sorting ideas are very important. When I encountered a question about reverse order, I used Merge Sorting and learned Merge Sorting.

Merge Sorting is based on the sub-governance idea. The sub-governance mode has three steps on each layer recursion:

Decomposition: divides n elements into subsequences containing n/2 elements.

Solution: Use the Merge Sorting method to recursively sort two subsequences.

Merge: merge two sorted subsequences to obtain the sorting result.

The key to algorithms is how to merge them. This is a clear introduction to algorithms.

Void merge (INT left, int middle, int right)
{

Int n1 = middle-left + 1; // number of elements from left to middle
Int n2 = right-middle; // number of elements from middle + 1 to right
For (INT I = 1; I <= N1; I ++)
L [I] = A [left + I-1]; // copy the left heap elements from left to middle to the L Array, sorted
For (INT I = 1; I <= n2; I ++)
R [I] = A [Middle + I]; // copy the elements in the right heap from middle + 1 to right to the r array.
L [N1 + 1] = m; // store the "Sentinel" at the bottom to avoid null comparison.
R [n2 + 1] = m;
Int I = 1; Int J = 1;
For (int K = left; k <= right; k ++) // copy the smallest elements in the two stacks to array a each time, and merge them into ordered sequences.
{
If (L [I] <= R [J])
{
A [k] = L [I];
I ++;
}
Else
{
A [k] = R [J];
J ++;
}
}

}

 

Recursive Merge Sorting. If there is at most one element in the sub-array, It is sorted out, otherwise it will be decomposed.

 

Void merge_sort (INT left, int right)
{
Int middle;
If (left <right)
{
Middle = (left + right)/2;
Merge_sort (left, middle); // returns the left part of binary splitting.
Merge_sort (middle + 1, right); // binary decomposition has partial
Merge (left, middle, right); // merge two parts
}

}

 

The algorithm always splits the sequence. Finally, the algorithm combines the bottom-up and top-up two sequences into sorted sequences, and then merges the sequence with the length of 2 into an ordered sequence with the length of 4, and so on. Merge them into an ordered sequence with a length of N.

 

After a slight modification in the Merge Sorting Algorithm, you can find the reverse order in the nlog2 (n) time.

Convert array a [1... size], divided into a [1... mid] And a [Mid + 1... size]. then the number of reverse logarithm is F (1, size) = f (1, mid)
+ F (Mid + 1, size) + S (1, mid, size), Here s (1, mid,
Size) indicates the reverse logarithm of the Left value in [1 --- mid] and the right value in [Mid + 1, size. Because both subsequences are sorted, It is very convenient to search for them.

Void merge (INT left, int middle, int right)
{

Int n1 = middle-left + 1;
Int n2 = right-middle;
For (INT I = 1; I <= N1; I ++)
L [I] = A [left + I-1];
For (INT I = 1; I <= n2; I ++)
R [I] = A [Middle + I];
L [N1 + 1] = m;
R [n2 + 1] = m;
Int I = 1; Int J = 1;
For (int K = left; k <= right; k ++)
{
If (L [I] <= R [J])
A [k] = L [I ++];
Else
{
A [k] = R [J ++];
Cunt + = n1-i + 1; // cunt is the global variable
}
}

}

 

Finally, cunt is the number of Reverse Order pairs.

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.