Use merge sort to find reverse order pairs and use merge sort to reverse order

Source: Internet
Author: User

Use merge sort to find reverse order pairs and use merge sort to reverse order

In reverse order, it is generally effective to use brute force solutions, but the time complexity of O (n2) is unacceptable. However, there is a seemingly irrelevant Algorithm for the reverse order pair problem to solve-Merge Sorting. The time complexity and space complexity are exactly the same as those of Merge Sorting, but a variable is added during the merge process, and the number of Reverse Order pairs is recorded. In this way, the time complexity is reduced to O (nlogn ).

Principle

Because the number of reverse-order pairs may have reverse-order pairs, to reduce the complexity of solving the number of reverse-order pairs to O (nlogn), you cannot calculate each reverse-order pair. According to the Merge Sorting idea, when we use merge to solve Reverse Order pairs, the number of Reverse Order pairs will be recorded in the process of merging two ordered arrays into an ordered array, others, such as array division and sorting, are sorted in the same way.

In the merging step of merging and sorting, assume that the two ordered arrays A [] and the ordered array B [] are combined into an ordered array C []. The Inverse Order of computing is converted to the inverse order of computing (a, B), where a comes from A [] and B comes from B []. When a <B, there is no count. When a> B (a, B) is A reverse order, because a [] is ordered, then, elements located after A in a [] form a reverse order pair for Element B in B [], so for reverse order pairs (A, B ), (assume that the starting subscript of A [] is sa, the ending subscript Is ea, And the subscript of a is pos.) In fact, after being merged into C [], the ea-pos + 1 reverse order pair will be generated.

That is to say, in the merge process, each time such a pair (a, B) appears, the number of backward Pairssum = sum + ea-pos+1 ;
Based on this principle, we will give an understanding of the Merge Sorting, and add the above calculation formula to the Merge Sorting. Then we can go to the O (nlogn) calculate the number of reverse pairs in a given number sequence in time complexity.

Sample Code
# Include <iostream> # define N 100000 using namespace std; void merge (int arr [], int start, int mid, int end, int temp [], long * count) {int index = 0, index1 = 0, index2 = 0; index = 0; index1 = start; index2 = mid + 1; while (index1 <= mid) & (index2 <= end) {if (arr [index1] <= arr [index2]) {temp [index ++] = arr [index1 ++];} else {temp [index ++] = arr [index2 ++]; // ans + = e1-p1 + 1; (* count) = (* count) + mid-index1 + 1 ;}while (index1 <= mid) temp [index ++] = arr [index1 ++]; while (index2 <= end) temp [index ++] = arr [index2 ++]; for (int I = 0; I <index; I ++) {// replication is also recursive, so it's not from start to end arr [start + I] = temp [I];} void mergeSort (int arr [], int start, int end, int temp [], long * count) {if (start <end) {// recursive exit int mid = 0; mid = (start + end)> 1; mergeSort (arr, start, mid, temp, count); mergeSort (arr, mid + 1, end, temp, count); merge (arr, start, mid, end, temp, count) ;}} int main () {long count = 0; int m = 0; cin> m; int * a = new int [m]; for (int I = 0; I <m; I ++) {cin> a [I];} int * temp = new int [m]; mergeSort (, 0 m-1, temp, & count); delete [] a; delete [] temp; cout <count <endl; return 0 ;}

Input Format: enter a number to indicate the length of the array, press enter, and then enter n Array elements separated by spaces. The output is the number of reverse pairs of the array.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.