public class Inversions extends Object
Inversions
class provides static methods to count the
number of inversions in either an array of integers or comparables.
An inversion in an array a[]
is a pair of indicies i
and
j
such that i < j
and a[i] > a[j]
.
This implementation uses a generalization of mergesort. The count operation takes Θ(n log n) time to count the number of inversions in any array of length n (assuming comparisons take constant time).
For additional documentation, see Section 2.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
Modifier and Type  Method and Description 

static long 
count(int[] a)
Returns the number of inversions in the integer array.

static <Key extends Comparable<Key>> 
count(Key[] a)
Returns the number of inversions in the comparable array.

static void 
main(String[] args)
Reads a sequence of integers from standard input and
prints the number of inversions to standard output.

public static long count(int[] a)
a
 the arrayi
and j
such that i < j
and a[i] > a[j]
.public static <Key extends Comparable<Key>> long count(Key[] a)
Key
 the inferred type of the elements in the arraya
 the arrayi
and j
such that i < j
and a[i].compareTo(a[j]) > 0
.public static void main(String[] args)
args
 the commandline arguments