Package edu.princeton.cs.algs4
Class BinarySearch
- Object
-
- edu.princeton.cs.algs4.BinarySearch
-
public class BinarySearch extends Object
TheBinarySearch
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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
indexOf(int[] a, int key)
Returns the index of the specified key in the specified array.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.
-
-
-
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 orderkey
- 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
-
-