Three kinds of methods for reverse order

Source: Internet
Author: User
Set A to an ordered set of n numbers (n>1), where all the numbers are different. If there is a positive integer i, J makes 1≤i < j≤n and a[i] > A[j], then <a[i], a[j]> this ordered pair called a in reverse order. the solution of inverse pairs first: bubbling (brute force)/enumeration directly bubbles the original sequence, counts the number of exchanges, and the number of exchanges = inverse logarithm. Note: The number of interchange of adjacent elements is equivalent to the inverse logarithm of the second sequence: the merge sort merges the original sequence, and the number of reverse pairs can be directly counted each time the two arrays are merged. For the element I in the left ordered sequence, and the element J in the right ordered sequence, if there is a[j] < A[i], then all elements after a[i] are greater than a[j]. In this case, the number of reverse pairs result = Mid-i + 1 The third: Discretization + Tree Array first uses discretization, reads all the data at once, then records the subscript and value of the reading data and sorts the values and subscripts by the size of the values. After the new data is obtained, a tree array with the new values (ordered ordinal numbers) is established, and the value of the tree array is the number of occurrences of that bit value in the current state. Each time you insert an element into a tree array, you use the tree array to query for the number of elements that are larger than the current value of the inserted element.

Three kinds of methods for reverse order

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.