O (N*LGN) time complexity in reverse order to realize the idea of statistical algorithm

Source: Internet
Author: User
Inverse pair definition: Set A[1..N] is an array that contains n different numbers. If, in the case of I<j, there is a[i] > A[j], then (I,J) is called an inverse pair (inversion) in a.
An algorithm is given to determine the number of inverse pairs in any permutation of n elements using the worst-case run time of O (N*LGN).

Simple Algorithm Realization idea: we can simply through the previous comparison to determine the number of reverse pairs, although the implementation is simple, but this time complexity will rise to O (n*n), does not meet our requirements.

Improved algorithm realization idea: It is well known that the time complexity of merge sort is O (Cn*lgn + cn), where C is constant. So we can achieve the goal by slightly improving the merging sort algorithm. That is, each time you merge two sub-arrays with the maximum value of the left sub-array as the reference pair to each element in the right sub-array, as long as the maximum value of the left sub-array is not copied back to the upper array, all the elements in the right array that have been copied to the upper array correspond to an inverse pair, when the left , if the right sub-array is not copied, the remaining elements in the right maximum sub-array are copied to the upper-level corresponding position, if the right sub-array has been copied, do nothing, and terminate the current layer in reverse order count. For example, there are two sub-sequences in a layer of a merge sort:
Left array: 2 5 9 12
Right array: 3 7 7 19
Since both the left and right arrays are ordered, all the elements of the current layer are copied back to the upper layers, resulting in a total of 3 reverse pairs, respectively, 3, 7, and 7 of the Rvalue array, and then the 19 in the right sub-array is copied to the upper layer. Finally, the total number of reverse pairs in the array can be counted by the layer processing.

Time Complex reading analysis: It is not difficult to find, the reverse of the time and merge the sort of roughly the same, for O (Cn*lgn + cn). If C equals 1, it is simplified to O (N*LGN + N), which is O (n (lgn + 1)).

O (N*LGN) time complexity in reverse order to realize the idea of statistical algorithm

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.