类 BinarySearch


  • public class BinarySearch
    extends java.lang.Object
    The BinarySearch class provides a static method for binary searching for an integer in a sorted array of integers.

    The indexOf operations takes logarithmic time in the worst case.

    For additional documentation, see Section 1.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

    • 方法概要

      修饰符和类型 方法 说明
      static int indexOf​(int[] a, int key)
      Returns the index of the specified key in the specified array.
      static void main​(java.lang.String[] args)
      Reads in a sequence of integers from the whitelist file, specified as a command-line argument; reads in integers from standard input; prints to standard output those integers that do not appear in the file.
      static int rank​(int key, int[] a)
      已过时。
      Replaced by indexOf(int[], int).
      • 从类继承的方法 java.lang.Object

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

      • indexOf

        public static int indexOf​(int[] a,
                                  int key)
        Returns the index of the specified key in the specified array.
        参数:
        a - the array of integers, must be sorted in ascending order
        key - the search key
        返回:
        index of key in array a if present; -1 otherwise
      • rank

        @Deprecated
        public static int rank​(int key,
                               int[] a)
        已过时。
        Replaced by indexOf(int[], int).
        Returns the index of the specified key in the specified array. This function is poorly named because it does not give the rank if the array has duplicate keys or if the key is not in the array.
        参数:
        key - the search key
        a - the array of integers, must be sorted in ascending order
        返回:
        index of key in array a if present; -1 otherwise
      • main

        public static void main​(java.lang.String[] args)
        Reads in a sequence of integers from the whitelist file, specified as a command-line argument; reads in integers from standard input; prints to standard output those integers that do not appear in the file.
        参数:
        args - the command-line arguments