If the array is greater than the number in front of the latter, it is the reverse of the
Method One: Using insertion sort, you can count the number of reverse exchange is the value of the
two: merge sort, you need to sacrifice space makes little to improve the time complexity
public static int InversePairs(int[] data) { if(data == null || data.length < 0) return 0; int[] copy = new int[data.length]; for(int i = 0; i < data.length; ++i) copy[i] = data[i]; return InversePairsCore(data, copy, 0, data.length - 1); } public static int InversePairsCore(int[] Data, int [] Copy, int Start, int End) { IF (== Start End) { Copy [Start] = Data [Start]; return 0 ; } int length = (End - Start) / 2 ; int left = InversePairsCore (Copy, Data, Start, Start + length); int right = InversePairsCore (Copy, Data, Start + length +. 1 , End); // I is initialized to the first half of the last digit of the index int I = Start + length; // J is initialized to the last half of the number in the subscript int J = End; int indexCopy = End; int COUNT = 0 ; the while (I> J = Start &&> = Start + length +. 1 ) { IF (Data [ I]> Data [J]) { Copy [indexCopy -] = Data [i-- ]; COUNT + = J - Start - length; } the else { Copy [indexCopy -] = Data [J, ]; } } for(; i >= start; --i) copy[indexCopy--] = data[i]; for(; j >= start + length + 1; --j) copy[indexCopy--] = data[j]; return left + right + count; }