Class BinarySearch


  • public class BinarySearch
    extends 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.

    Author:
    Robert Sedgewick, Kevin Wayne
    • Method Detail

      • indexOf

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

        public static void main​(String[] args)
        Reads in a sequence of integers from the allowlist 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.
        Parameters:
        args - the command-line arguments