The sword refers to the offer surface question (Java Edition): The reverse order in the array

Source: Internet
Author: User

Title: Two numbers in an array if the previous number is greater than the number that follows, then these two numbers form an inverse pair. Enter an array to find the total number of reverse pairs in this array

For example, in the array {7,5,6,4}, there are altogether 5 pairs of reverse pairs, respectively {7,6},{7,5},{7,4},{6,4},{5,4}.

Seeing this topic, our first response is to scan the entire array sequentially. Each time an array is scanned, the size of the number and the number behind it is compared. If the subsequent number is smaller than it, then these two numbers form an inverse pair. Suppose the array contains n digits. Since each number is compared to an O (n) number, the time complexity of the algorithm is O (N2). We're trying to find a faster algorithm.

We use the array {7,5,6,4} as an example to analyze the process of statistical reverse pairs, each time we scan to a number, we can not compare it with each of the following numbers, otherwise the time complexity is O (N2) So we can consider the first comparison of two adjacent numbers.

As shown, we first divide the array into two sub-arrays of length 2, and then the two sub-arrays are two sub-arrays of length 1 in Tea city. The next side merges adjacent sub-arrays, counting the number of reverse pairs. In the first pair of sub-arrays of length 1 {7},{5}, 7 is greater than 5, so {7,5} consists of an inverse pair. Similarly, in the second pair of sub-arrays of length 1 {6},{4}, there is also an inverse pair of {6,4}. Since we have counted the two pairs of sub-arrays in reverse order, we need to sort the two pairs to avoid repeating the statistics in the subsequent statistical process.


Next we count the inverse pairs between two sub-arrays of length 2.

We first point to the end of two sub-arrays with two pointers, and compare the numbers pointed to by two pointers at a time. If the number in the first subarray is greater than the number in the second subarray, the inverse pair is formed, and the number of reverse pairs is equal to the number of remaining digits in the second sub-array. If the number in the first array is less than or equal to the number in the second array, it does not form a reverse pair. Each time we compare, we copy the larger numbers from the back to the secondary array, making sure the numbers in the auxiliary array are ascending. After copying the larger number to the array, move the corresponding pointer forward one bit, then proceed to the next round of comparisons.

After the detailed discussion above, we suspect summed up the process of statistical reverse order: First, the array is separated into sub-arrays, the number of reverse pairs in the sub-array is counted, and then the number of reverse pairs between two adjacent sub-arrays is counted. In the process of counting reverse pairs, you also need to sort the arrays. If we are familiar with the sorting algorithm, it is not difficult to find that this sort of process is the merge sort.

Implement the above procedure with Java code

/** * Reverse Order: * Two digits in the array if the previous number is greater than the next number, the two numbers form an inverse pair. * Enter an array to find the total number of reverse pairs in this array.  */package swordforoffer;import java.util.arraylist;/** * @author Jinshuangqi * * August 9, 2015 */public class E36inversepairs {Private arraylist<integer> assignlist (arraylist<integer> list, int start,int end) {Arraylist<integer > des = new arraylist<integer> (); for (int i = start;i<end;i++) {Des.add (List.get (i));} Return des;} Public long mergetwolist (arraylist<integer> list,int start,int half,int end) {Long Count = 0; arraylist<integer> templeft = assignlist (list,start,half); arraylist<integer> tempright = assignlist (list,half,end); int leftindex = 0;int Rightindex = 0;int index = Start;whil E (Leftindex < Templeft.size () && rightindex <tempright.size ()) {int temp1 = Templeft.get (leftindex); int Temp2 = Tempright.get (Rightindex), if (Temp1 > Temp2) {count+=templeft.size ()-Leftindex;list.set (index, TEMP2); index++;rightindex++;} Else{list.set (index, TEMP1); index++;leftindex++;}} for (; Leftindex < Templeft.size (); leftindex++) {list.set (index, Templeft.get (Leftindex)); index++;} for (; Rightindex <tempright.size (); rightindex++) {list.set (index, Tempright.get (Rightindex)); index++;} return count;} Public long getinversions (arraylist<integer> list,int start,int end) {Long Count = 0;if ((end-start) <= 1) return 0 ; int half = start+ (end-start)/2;count + = Getinversions (list,start,half); count + = Getinversions (list,half,end); count + = Mergetwolist (list,start,half,end); return count;} Public long Getinversepairs (int[] arr) {arraylist<integer> al = new arraylist<integer> (); for (int i = 0;i< arr.length;i++) {Al.add (arr[i]);} int end =arr.length;return getinversions (al,0,end);} public static void Main (string[] args) {int[] arr={7,5,6,4}; E36inversepairs test = new E36inversepairs (); System.out.println (Test.getinversepairs (arr));}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The sword refers to the offer surface question (Java Edition): The reverse order in the array

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.