Package dsa

Class 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].
    • Constructor Detail

      • Inversions

        public Inversions()
    • Method Detail

      • count

        public static long count​(Comparable[] a)
        Returns the number of inversions in the array a, according to the natural order of its objects.
        Parameters:
        a - the array.
        Returns:
        the number of inversions in the array a, according to the natural order of its objects.
      • count

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