Algorithm design "divide and conquer law" merge sort (iii) Reverse logarithm of case analysis

Source: Internet
Author: User

Problem definition:

Suppose A[1...N] is an array with n different numbers. If I<j and A[i]>a[j] is called (A[i], a[j]) is an inverse pair for the array A.

For example arrays <2, 3, 8, 6, 1> have (2, 1), (3, 1), (8, 6), (8, 1), and (6,1) 5 reverse pairs.

To solve this problem intuitively, using the brute force solution, the time complexity is O (n^2) for every number num to traverse all the numbers in the array after Num.

The implementation code is as follows:

Another way, is to use the partition method, the entire array is first divided into two parts, and then, the number of two subgroups to solve the number of reverse order, when two sub-arrays have only one number, you can directly get two number is reverse or non-reverse order.

For example: the element information in an array is: <3, 2, 8, 1>, then the recursive process is as follows:

0. First Initialize CNT = 0;

1. First divide the array into two parts <3, 2> <8, 1>

2. For <3, 2> continues to decompose into <3> <2>

3. When decomposition to two sub-arrays have only one element: two sub-arrays are sorted, then <3> <2>, you can directly judge 3>2, so CNT + +. and merge two sub-arrays into an ordered array <2, 3>

4. At the same time for 1 neutron array <8, 1> decomposition to <8> <1>, can be directly judged 8>1, so CNT + +. and merge two sub-arrays into an ordered array <1, 8>

5. When backtracking to two sub-arrays are: a:<2, 3> b:<1, 8>, at this point in order to calculate the number of reverse, only need for two ordered sub-arrays when merging, for the merge code to modify: Because Index=0, a[index]>b[0] , so that the elements in the a array after 2 are greater than b[0], then cnt + = (n1-index), N1 represents the length of the left array A.

The following benefits are obtained by using the divide-and-conquer method for reverse number:

1. Time Complexity of O (NLG (n))

2. You can also sort the entire array while retrieving the number of reverse orders.

However, there is a common problem with divide-and-conquer method, which is that although the time complexity is reduced, a secondary storage space is needed.

With the above analysis, the source code for the inverse logarithmic statistics is as follows:

The code here is the same as the merge sort algorithm implementation code is basically the same, the key is in and the part of the red line, for the inverse logarithm of the statistics.

Algorithm design "divide and conquer law" merge sort (iii) Reverse logarithm of case analysis

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.