Package dsa
Class Inversions
java.lang.Object
dsa.Inversions
This library to count the number of inversions in an array. An inversion in an array
a
is a pair
(i, j)
of indicies such that i
< j
and a[i]
> a[j]
.-
Method Summary
Modifier and TypeMethodDescriptionstatic long
count
(double[] a) Returns the number of inversions in the arraya
.static long
count
(int[] a) Returns the number of inversions in the arraya
.static <T extends Comparable<T>>
longcount
(T[] a) Returns the number of inversions in the arraya
, according to the natural order of its objects.static <T> long
count
(T[] a, Comparator<T> c) Returns the number of inversions in the arraya
, according to the order induced by the comparatorc
.static void
Unit tests the library.
-
Method Details
-
count
Returns the number of inversions in the arraya
, according to the natural order of its objects.- Type Parameters:
T
- the type of items in a.- Parameters:
a
- the array.- Returns:
- the number of inversions in the array
a
, according to the natural order of its objects.
-
count
Returns the number of inversions in the arraya
, according to the order induced by the comparatorc
.- Type Parameters:
T
- the type of items in a.- Parameters:
a
- the array.c
- the comparator to determine the order of the array.- Returns:
- the number of inversions in the array
a
, according to the order induced by the comparatorc
.
-
count
public static long count(int[] a) Returns the number of inversions in the arraya
.- Parameters:
a
- the array.- Returns:
- the number of inversions in the array
a
.
-
count
public static long count(double[] a) Returns the number of inversions in the arraya
.- Parameters:
a
- the array.- Returns:
- the number of inversions in the array
a
.
-
main
Unit tests the library.- Parameters:
args
- the command-line arguments.
-