类 Inversions


  • public class Inversions
    extends java.lang.Object
    The 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 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>>
      long
      count​(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.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 方法详细资料

      • 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 and j such that i < j and a[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 and j such that i < j and a[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