[Merge Sorting]-Find the reverse ordinal algorithm

1. Merge sort

  • Merging and sorting is a typical application of divide and conquer method, applying recursive thinking, thinking from top to bottom: first assume that MergeSort()an out-of-order array can be sorted, so you can start (divide an array into two parts on average), and then (respectively The call MergeSort()makes the two parts sorted), and finally use to Merge()merge the two sorted arrays into one sorted array.
  • Merge()The method is simple to implement. You only need to manage two pointers, which point to the two arrays to be merged, and develop an auxiliary array to save the intermediate results. The O(n)time complexity can be completed

2. Reverse order number

  • Definition of reverse ordinal number: if i < jand A[i] > A[j]then the A[i]sum is A[j]the reverse ordinal number pair. The number of pairs in reverse order is called reverse order
  • Finding the ordinal number can be obtained by managing two pointers, scanning the array twice, and using brute force methods. Obviously the time complexity is Θ(n^2).
  • Using the merge sort method, you can make a little improvement. In Merge(), merge two already ordered arrays A and B. Because AB is ordered, the reverse order number of A and B is 0, so the reverse order number of AB is equal to the reverse order number between A and B.
  • For example: A=1,4,6,7,9, . B=2,3,5,10,13,21Merge found that the current number of the element 4 i greater than 2, then the required number of reverse 4 + 1, because the back 4 are ranked in the 6,7,9, 6,7,9 then the reverse order of the number of It should also be +1, so the overall reverse order number should be added last-i+1.
    Insert picture description here
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
    public int InversePairs(int[] array) {
        int len = array.length;
        int[] c = new int[len];
        count = 0;
        MergeSort(array, 0, len - 1, c);
        return count;
    }

    public static int MOD = 1000000007;
    public static int count = 0;//新增

    public static void Merge(int[] array, int left, int mid, int right, int[] c) {
        int i = left;
        int j = mid + 1;
        int k = left;
        while (i <= mid && j <= right) {
            if (array[i] <= array[j])
                c[k++] = array[i++];
            else {
                c[k++] = array[j++];
                //count += mid - i + 1;//新增
                count = (count + mid - i + 1) % MOD;
            }
        }
        while (i <= mid)
            c[k++] = array[i++];
        while (j <= right)
            c[k++] = array[j++];
        //C数组已经有序,将数组复制回原数组
        for (int in = left; in <= right; in++) {
            array[in] = c[in];
        }
    }

    public static void MergeSort(int[] array, int left, int right, int[] c) {
        if (left < right) {
            int mid = (left + right) / 2;
            MergeSort(array, left, mid, c);   //将第一个数组排好
            MergeSort(array, mid + 1, right, c);    //将第二个数组排好
            Merge(array, left, mid, right, c);     //合并两个有序数组
        }
    }
}
Published 395 original articles · won 130 · 200,000 views +

Guess you like

Origin blog.csdn.net/qq_40507857/article/details/103802860