Calculate the number of Reverse Order pairs (inversions)

Source: Internet
Author: User

Reverse Order: Set A to an array with n different elements. If I <j, a [I]> A [J], (I, j) is called a reverse order of.

Given an array and finding the number of Reverse Order pairs, we can use a loop to find the number of Reverse Order pairs in the time of N ^ 2, but usually in the nlog (N) only after the number of Reverse Order pairs is obtained, the array is sorted. If you do not want to change the original array, you can copy the original array to another array, operate on the new array. If there is a number in the deepest part of the recursion, the return value is 0. If there are two numbers, it is considered as a subarray with two elements, both sub-arrays are sorted, and the reverse direction of the sub-array with only one element on the left is 0, and the reverse direction of the sub-array with only one element on the right is 0, then the number of reverse pairs of the sub-arrays of the two elements is 1. If the two values are in reverse order, otherwise the number is 0. So when recursion goes to the last step, that is: A [1] to a [n/2] has sorted the order, and the number of Reverse Order pairs is inv1, A [n/2 + 1] to a [n] has sorted the order, and the number of Reverse Order pairs is inv2, the total number of reverse pairs of the entire array is the reverse logarithm of inv1 + inv2 + during the last merge process. So how do we calculate the number of reverse pairs in the merge step? If I is the subscript of the child array on the left (the range of I is 1 to mid), J is the subscript OF THE CHILD array on the right (the range of J is Mid + 1 to n ), assume that a pair (I, j) has a [I]> A [J], then a [I] to a [Mid] is greater than a [J], the inverse logarithm of contribution is mid-I + 1.

# Include <stdio. h> <br/> # include <stdlib. h> <br/> int Merge (int * a, int low, int mid, int high) <br/>{< br/> int n1 = mid-low + 1; <br/> int n2 = high-mid; <br/> int * tmp1 = malloc (N1 * sizeof (INT )); <br/> int * tmp2 = malloc (N2 * sizeof (INT); <br/> int I, j, k; <br/> for (I = 0; I <N1; I ++) <br/> tmp1 [I] = A [Low + I]; <br/> for (I = 0; I <N2; I ++) <br/> tmp2 [I] = A [Mid + I + 1]; <br/> K = low; <br/> int cross_inversion = 0; <br/> for (I = 0, j = 0; I <N1 & J <N2 ;) <br/> {<br/> If (tmp1 [I] <= tmp2 [J]) <br/> A [k ++] = tmp1 [I ++]; <br/> else <br/> {<br/> // from I to n1-1, so it is n1-1-i + 1 = n1-i Reverse Order pairs <br/> A [K + +] = tmp2 [J + +]; <br/> cross_inversion + = (n1-i ); <br/>}< br/> while (I <N1) <br/> A [k ++] = tmp1 [I ++]; <br/> while (j <N2) <br/> A [k ++] = tmp2 [J ++]; <br/> free (tmp1 ); <br/> free (tmp2); <br/> return cross_inversion; <br/>}< br/> int count_inversions (int * a, int low, int high) <br/>{< br/> int inversions = 0; <br/> If (low <pigh) <br/>{< br/> int mid = (low + high)/2; <br/> inversions + = count_inversions (A, low, mid ); <br/> inversions + = count_inversions (A, Mid + 1, high); <br/> inversions + = Merge (A, low, mid, high ); <br/>}< br/> return inversions; <br/>}< br/> int main (void) <br/>{< br/> int A [] = {8, 7, 6, 5, 4, 3, 2, 1 }; <br/> int n = sizeof (a)/sizeof (A [0]); <br/> printf ("inversions: % d/N", count_inversions (, 0, n-1); <br/> return 0; <br/>}

 

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.