The reverse order in the array

Source: Internet
Author: User

Two numbers in the array if the previous number is greater than the number that follows, the two numbers form an inverse pair, enter an array, and find the total number of reverse pairs in the array. For example: {7,5,6,4}, there are altogether 5 reverse pairs, respectively (7,6) (7,5) (7,4) (6,4) and (5,4)
Thinking is no idea, generally used to brute force, the sword refers to the idea of the merger of the post of offer, let me think that the merger sort was written many years ago (forget ...) Sure enough to get out of the mix is always the same). The idea of merging sorts is to use the divide-and-conquer method to narrow down a very large problem into a very small problem, to compare a very long array and finally recursively into two numbers. Reverse to just can take advantage of this point, when the remaining two number of time, to determine whether the front is greater than the back, if greater than the back, then is a reverse order, and the following is generally ordered, so we let the pointer from the back to the beginning of the traversal, as long as the first half of the data is greater than the last one, Then it will be larger than all the second half.

(Image from: http://blog.csdn.net/cjf_iceking/article/details/7920153)
After the first layer is ordered, back to the second layer, when we judge the size of 95 and 84, 95>84, that is 95 is greater than 84 all, so at this time the reverse logarithm is 84 plus all the elements before the number. Then after the calculation is complete, the new array is put back, so that the back will not accumulate again, this actually borrowed the idea of merging sort.
Code
public static int Inversepair (int[] data,int start,int end,int[] temp) {int count=0;  Total number of cumulative reverse pairs if (Start < end) {    int mid = (End+start)/2;   Count + =  Inversepair (data, start, Mid, temp);   Count + =  Inversepair (data,mid+1,end,temp);   Count + = Getinversepair (data,start,mid,end,temp);  }    return count;} private static int Getinversepair (int[] data, int start,int mid, int end,int[] temp) {int i=mid;    The end of the first half of the int count=0; int j = END;  End of the second half of int index=0;while (i >=start && j>mid) {if (Data[i] > Data[j]) {temp[index++] = Data[i--];count + = J-mid;  All elements prior to J are reverse logarithm}else{temp[index++] = data[j--];}} while (I>=start) {temp[index++] = data[i--];} while (J>mid) {temp[index++] = data[j--];} Put it in the order of the original array for (int k=0;k<index;k++) {Data[end-k] = temp[k];}   return count;}




Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

Reverse-order pairs in an 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.