Package dsa

Class Inversions

java.lang.Object
dsa.Inversions

public class Inversions extends Object
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 Type
    Method
    Description
    static long
    count(double[] a)
    Returns the number of inversions in the array a.
    static long
    count(int[] a)
    Returns the number of inversions in the array a.
    static <T extends Comparable<T>>
    long
    count(T[] a)
    Returns the number of inversions in the array a, according to the natural order of its objects.
    static <T> long
    count(T[] a, Comparator<T> c)
    Returns the number of inversions in the array a, according to the order induced by the comparator c.
    static void
    main(String[] args)
    Unit tests the library.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • count

      public static <T extends Comparable<T>> long count(T[] a)
      Returns the number of inversions in the array a, 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

      public static <T> long count(T[] a, Comparator<T> c)
      Returns the number of inversions in the array a, according to the order induced by the comparator c.
      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 comparator c.
    • count

      public static long count(int[] a)
      Returns the number of inversions in the array a.
      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 array a.
      Parameters:
      a - the array.
      Returns:
      the number of inversions in the array a.
    • main

      public static void main(String[] args)
      Unit tests the library.
      Parameters:
      args - the command-line arguments.