类 Inversions
- java.lang.Object
-
- edu.princeton.cs.algs4.Inversions
-
public class Inversions extends java.lang.Object
TheInversions
class provides static methods to count the number of inversions in either an array of integers or comparables. An inversion in an arraya[]
is a pair of indiciesi
andj
such thati < j
anda[i] > a[j]
.This implementation uses a generalization of mergesort. The count operation takes time proportional to n log n, where n is the number of keys in the array.
For additional documentation, see Section 2.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
方法概要
修饰符和类型 方法 说明 static long
count(int[] a)
Returns the number of inversions in the integer array.static <Key extends java.lang.Comparable<Key>>
longcount(Key[] a)
Returns the number of inversions in the comparable array.static void
main(java.lang.String[] args)
Reads a sequence of integers from standard input and prints the number of inversions to standard output.
-
-
-
方法详细资料
-
count
public static long count(int[] a)
Returns the number of inversions in the integer array. The argument array is not modified.- 参数:
a
- the array- 返回:
- the number of inversions in the array. An inversion is a pair of
indicies
i
andj
such thati < j
anda[i] > a[j]
.
-
count
public static <Key extends java.lang.Comparable<Key>> long count(Key[] a)
Returns the number of inversions in the comparable array. The argument array is not modified.- 类型参数:
Key
- the inferred type of the elements in the array- 参数:
a
- the array- 返回:
- the number of inversions in the array. An inversion is a pair of
indicies
i
andj
such thati < j
anda[i].compareTo(a[j]) > 0
.
-
main
public static void main(java.lang.String[] args)
Reads a sequence of integers from standard input and prints the number of inversions to standard output.- 参数:
args
- the command-line arguments
-
-