In Java, Arrays.binarySearch(a, key) searches a sorted array by repeatedly halving the part that could contain key. It needs about log2(n) comparisons, so even a million elements take only about 20 steps.
If the key is found, the result is its index. If not, the result is negative: -(insertionPoint) - 1, where the insertion point is the index at which the key would have to be inserted to keep the array sorted. The - 1 makes sure "not found" is never 0.
A static method of java.util.Arrays (Java 1.2+): call it on the class, as in Arrays.binarySearch(...); there is no Arrays object to create. The range overloads were added in Java 6.
Syntax
Sort first, then search the whole array or a range.
Java
int[] a = {10, 20, 30, 40}; // must be sorted
int i = Arrays.binarySearch(a, 30); // 2
int j = Arrays.binarySearch(a, 25); // -3 (insertion point 2)
int k = Arrays.binarySearch(a, 1, 3, 40); // -4 (searches indexes 1 and 2)
Call
What it does
Arrays.binarySearch(a, key)
Search the whole sorted array.
Arrays.binarySearch(a, from, to, key)
Search indexes from to to - 1 only.
Arrays.binarySearch(objs, key, comparator)
Search an array sorted by that comparator.
-result - 1
Turns a negative result back into the insertion point.
The pair (a, key) and (a, from, to, key) exists for int, long, short, char, byte, float, double, and Object arrays; the Comparator versions exist for T[].
Library declarations
Java
// java.util.Arrays (since Java 1.2; ranges since Java 6)
public static int binarySearch(int[] a, int key)
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
public static int binarySearch(Object[] a, Object key)
public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)
public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
// same pair for long[], short[], char[], byte[], float[], double[]
Parameters
Parameter
Description
a
The array to search, sorted in ascending order (or by c).
fromIndex, toIndex (optional)
Range to search: fromIndex inclusive, toIndex exclusive.
key
The value to find.
c (optional)
The comparator the array was sorted with.
Return Value
Return Value
Description
int ≥ 0
The index of an element equal to key.
int < 0
-(insertionPoint) - 1; the insertion point is the index of the first larger element, or a.length (or toIndex) if all are smaller.
Look up a score that exists and one that does not, and decode the negative result.
Java
import java.util.Arrays;
public class ArraysBinarySearchDemo {
public static void main(String[] args) {
int[] scores = {12, 25, 38, 47, 59, 63};
int found = Arrays.binarySearch(scores, 47);
System.out.println("47 at index " + found);
int missing = Arrays.binarySearch(scores, 50);
int insertAt = -missing - 1;
System.out.println("50 -> " + missing + ", would go at index " + insertAt);
}
}
Output
47 at index 3
50 -> -5, would go at index 4
How It Works
Sorted input. The scores are already in ascending order, which binary search requires.
A hit. 47 is at index 3, so that index is returned directly.
A miss. 50 would belong between 47 and 59, at index 4, so the method returns -(4) - 1 = -5.
Decode it.-missing - 1 turns -5 back into 4, the index where 50 could be inserted to keep the order.
Example 2 — Sort a String Array Before Searching
Fruit names arrive unsorted; sort them, then search for one present and one missing name.