Directory
1 Problem Description
2 Solutions
2.1 Brute Force method
2.2 Division Method (Merge sort)
1 problem description
Given a random number array, the total number of inverse pairs in this array is obtained. Require time efficiency as high as possible.
So, what's the reverse order?
Quoted from Baidu Encyclopedia:
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 is called a in reverse order, also known as the inverse number. For example, the inverse pairs of arrays (3,1,4,5,2) have (3,1), (3,2), (4,2), (5,2), a total of 4.
2 Solutions 2.1 Brute Force method
In a preliminary look, the use of brute force is the most direct and simplest method, but the time efficiency is O (n^2).
that is, from the first 1 elements, the beginning and then each of the following elements of the size comparison, if greater than, then the number of reverse order plus 1.
The specific code is as follows:
PackageCom.liuzhen.systemExe; Public classmain{//finding the inverse logarithm in array A by brute force method Public intBrutereversecount (int[] A) {intresult = 0; for(inti = 0;i < a.length;i++) { for(intj = I;j < a.length;j++) { if(A[i] >a[j]) result++; } } returnresult; } //gets an array of random numbers Public int[] Getrandomarray (intN) {int[] result =New int[n]; for(inti = 0;i < n;i++) {Result[i]= (int) (Math.random () * 50);//Generaterandom number between 0~50 } returnresult; } Public Static voidMain (string[] args) {LongT1 =System.currenttimemillis (); Main Test=NewMain (); int[] A = Test.getrandomarray (50000); intresult =Test.brutereversecount (A); Longt2 =System.currenttimemillis (); System.out.println ("Using brute force method to get results:" +result+ ", Time-consuming:" + (T2-T1) + "milliseconds"); }}
Run results (run 3 times):
Using brute force method to get results: 612226389, Time: 8094 ms Get results using brute force method:610311942, Time: 8015 MS Results obtained using brute force method: 610657465, Time: 8079 ms
2.2 Division method (Merge sort)
In addition to the brute force method, the idea of merging sorting can be used here to solve the problem, when the time complexity is O (N*LOGN). Merge sort, in which the first half is divided until the last left half of the array has only one element, and the right half of the array has only one element, then the backtracking merge begins. Then, the key to calculate the number of reverse order, it is here in the backtracking merge process, when the left half of the element (PS: The left half and the right half of the elements are ascending sort) in the upper right half of the element, then the left half of this element and all the elements behind it is greater than the right half of the element, Remember that the number of these elements is len, then the number of reverse order to self-increment len.
The specific code is as follows:
PackageCom.liuzhen.systemExe; Public classmain{ Public LongCount = 0;//global variables, using merge sort, to calculate inverse logarithm//using the merge sort method to calculate the inverse logarithm in array a Public voidGetreversecount (int[] A) {if(A.length > 1) { int[] Lefta = Gethalfarray (A, 0);//left half element of array a int[] Righta = Gethalfarray (A, 1);//right half element of array aGetreversecount (LEFTA); Getreversecount (Righta); Mergearray (A, Lefta, Righta); } } //based on the judge value, gets the left half element of array A or the right half element Public int[] Gethalfarray (int[] A,intjudge) { int[] result; if(judge = = 0) {//returns the left half of the array aresult =New int[A.LENGTH/2]; for(inti = 0;i < a.length/2;i++) Result[i]=A[i]; } Else{//returns the right half of the arrayresult=New int[A.LENGTH-A.LENGTH/2]; for(inti = 0;i < a.length-a.length/2;i++) Result[i]= A[a.length/2 +i]; } returnresult; } //merges the left and right half elements of array A and arranges them in a non-descending sequence Public voidMergearray (int[] A,int[] Lefta,int[] righta) { intLen = 0; inti = 0; intj = 0; intLENL =lefta.length; intLENR =righta.length; while(I < lenl && J <lenr) { if(Lefta[i] >Righta[j]) {A[len+ +] = righta[j++];//Place Righta[j] before the lefta[i] element, then lefta[i] after lenl-i elements are greater than righta[j]Count + = (lenl-i);//before merging, the elements in the LEFTA are in a non-descending order, and the elements in the Righta are not in descending order. So, at this point, add lenl-i a reverse order to}Else{A[len+ +] = lefta[i++]; } } while(I <lenl) A[len+ +] = lefta[i++]; while(J <lenr) A[len+ +] = righta[j++]; } //gets an array of random numbers Public int[] Getrandomarray (intN) {int[] result =New int[n]; for(inti = 0;i < n;i++) {Result[i]= (int) (Math.random () * 50);//Generaterandom number between 0~50 } returnresult; } Public Static voidMain (string[] args) {LongT1 =System.currenttimemillis (); Main Test=NewMain (); int[] A = Test.getrandomarray (50000); Test.getreversecount (A); Longt2 =System.currenttimemillis (); System.out.println ("Divide and Conquer" results: "+test.count+", Time-consuming: "+ (T2-T1) +" milliseconds "); }}
Run results (run 3 times):
Divide and conquer the result: 612226489, Time: 36 millisecond Divide the result:610481152, time: 35 millisecond Divide the methodto obtain the result: 612161208, time: 32 milliseconds
Resources:
1. Merge sort to reverse order
Algorithm Note _065: Split-ruling method for reverse order (Java)