The inverse of the algorithm

Source: Internet
Author: User
Tags what array

The inverse of the algorithm to the problem of reverse order

? Suppose A[1..N] is an array with n different numbers. If I a[j], then the duality (I, j) is called an inverse pair (inversion) of a.

  1. Lists the 1 reverse pairs of the array {2, 3, 8, 6, 5}
  2. What array of elements in the collection {1, 2, ..., n} has the most inverse pairs? How many reverse pairs does it have?
  3. How does the run time of the insert sort relate to the number of reverse pairs in the input array?
  4. An algorithm for calculating the number of inverse pairs in any permutation of n elements is given, and the worst time complexity is: \ (\theta\)(NLGN)
    1. Depending on the definition, the reverse order is: (2, 1), (3, 1), (8, 6), (8, 1), (6, 1)

    2. When an array element has exactly n elements arranged from large to small, it has the most inverse pairs. At this point the reverse is: (n-1) + (n-2) + (n-3) + ... + 1 = n (n-1)/2

    3. Depending on the implementation of the insertion sort, it is not difficult to get the number of times that the desired number of moves, which is the inverse pair of the right value with ARR[J], is required to insert the sorted array each time the array is never sorted by selecting a value arr[j]. This feature can also be designed with a time complexity of: \ (\theta\)(\ (n^2\)) algorithm. Of course, this algorithm of exponential level complexity we pass directly

    4. It is not difficult to think of (\theta\)(NLGN) algorithm complexity of the merge sort. In fact, the merge sort does not change the number of reverse pairs when divided. It is only when merging that the right side is merged into the original array because of the appearance of reverse order. In fact, the revision point is mainly in two areas:

    • Declares a global variable used to store the total number of times
    • Accumulate the total number of times when the right hand is in the original array (merge method is only needed to merge and sort, please refer to http://www.cnblogs.com/Kidezyq/p/8379267.html)

The specific source code is as follows:

Private Static intCountPrivate Static void Merge(int[] arr,intStartIndex,intMidindex,intEndIndex) {/*** Merge Policy:* 1. Create a new two array, and access the left half of the ordered array and the right half of the sorted array* 2. Starting from the left and right two arrays to start the index, select the smaller one to put the original array corresponding position* 3. Finally, if one of the left and right arrays has been traversed, the remaining elements of the other array are placed directly behind the group of elements.     */    //STEP1    int[] Leftarr =New int[Midindex-startindex];int[] Rightarr =New int[Endindex-midindex]; System.arraycopy(arr, StartIndex, Leftarr,0, Leftarr.length); System.arraycopy(arr, Midindex, Rightarr,0, Rightarr.length);//STEP2    intK = StartIndex;//Store subscript in the original array    inti =0;//store subscript of the left array    intj =0;//store subscript for the right array     while(I < Leftarr.length&& J < Rightarr.length) {//Copy smaller elements to the group of elements corresponding to subscript k, and move the array of smaller elements with subscript        if(Leftarr[i] < rightarr[j])            {arr[k++] = leftarr[i++]; }Else{//At this point the value of arr[i] to arr[leftarr.length-1] is larger than arr[j], that is, the number of reverse order at the end of Arr[j] is:Count + = Leftarr.lengthI        arr[k++] = rightarr[j++]; }    }//STEP3    if(I < Leftarr.length) {System.arraycopy(Leftarr, I, arr, K, Leftarr.)length-i); }Else if(J <= Rightarr.length) {System.arraycopy(Rightarr, J, arr, K, Rightarr.)length-j); }}

The inverse of the 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.