A search algorithm finds whether a value is present in a collection and where. When the data is sorted, there is a much faster option than checking every element: binary search, which halves the remaining search range with every comparison.
What is a Binary Search Algorithm?
Binary search looks for a target value in a sorted array by comparing it with the middle element of the current range:
- if they are equal, the search is over;
- if the target is smaller, it can only be in the left half, so the right half is discarded;
- if the target is larger, it can only be in the right half, so the left half is discarded.
The search repeats on the remaining half until the target is found or the range becomes empty, which means the target is not present.

Because each step halves the range, an array of n elements needs at most about log2 n + 1 comparisons: about 20 for a million elements, compared with up to a million for linear search.
Binary search is implemented using following steps
- Set
low = 0andhigh = n − 1; the target can only be inarray[low..high]. - While
low <= high, compute the middle indexmid = low + (high − low) / 2. - If
array[mid]equals the target, returnmid. - If
array[mid]is smaller than the target, continue withlow = mid + 1. - Otherwise continue with
high = mid − 1. - If the loop ends because
low > high, the target is not present; return −1.
Writing the middle index as low + (high - low) / 2 instead of (low + high) / 2 avoids an int overflow when low + high exceeds Integer.MAX_VALUE (Java integer arithmetic silently wraps around).
Binary Search Implementation in Java
Binary search can be written in Java in four common ways:
- Iterative Implementation
- Recursive Implementation
- Using Arrays.binarySearch()
- Using Collections.binarySearch()
1. Iterative Implementation
A loop narrows the range until the key is found or the range is empty. It uses O(1) extra space.
// Iterative binary search on a sorted int array
public class BinarySearchIterative {
// Returns the index of key in the sorted array, or -1 if it is absent
static int binarySearch(int[] array, int key) {
int low = 0;
int high = array.length - 1;
while (low <= high) { // the range low..high is not empty
int mid = low + (high - low) / 2; // no int overflow, unlike (low + high) / 2
if (array[mid] == key) {
return mid;
} else if (array[mid] < key) {
low = mid + 1; // key can only be in the right half
} else {
high = mid - 1; // key can only be in the left half
}
}
return -1; // range became empty: not found
}
static void report(int[] array, int key) {
int index = binarySearch(array, key);
if (index == -1) {
System.out.println("Element " + key + " is not present in array");
} else {
System.out.println("Element " + key + " is present at index " + index);
}
}
public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50, 60, 70, 80}; // must be sorted
report(array, 40);
report(array, 250);
}
}
Element 40 is present at index 3 Element 250 is not present in array
2. Recursive Implementation
The same idea written as a method that calls itself on one half. It is easy to read but uses O(log n) stack space.
// Recursive binary search on a sorted int array
public class BinarySearchRecursive {
// Searches array[low..high]; returns the index of key, or -1
static int binarySearch(int[] array, int low, int high, int key) {
if (low > high) {
return -1; // empty range: not found
}
int mid = low + (high - low) / 2;
if (array[mid] == key) {
return mid;
}
if (array[mid] < key) {
return binarySearch(array, mid + 1, high, key); // search the right half
}
return binarySearch(array, low, mid - 1, key); // search the left half
}
static void report(int[] array, int key) {
int index = binarySearch(array, 0, array.length - 1, key);
if (index == -1) {
System.out.println("Element " + key + " is not present in array");
} else {
System.out.println("Element " + key + " is present at index " + index);
}
}
public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50, 60, 70, 80}; // must be sorted
report(array, 40);
report(array, 250);
}
}
Element 40 is present at index 3 Element 250 is not present in array
3. Using Arrays.binarySearch()
The JDK method java.util.Arrays.binarySearch() searches a sorted array. If the key is found it returns its index; otherwise it returns −(insertion point) − 1, a negative number from which you can recover where the key would be inserted. If the array is not sorted, the result is undefined.
import java.util.Arrays;
// Binary search with the JDK method Arrays.binarySearch()
public class ArraysBinarySearchExample {
static void report(int[] array, int key) {
int result = Arrays.binarySearch(array, key);
if (result >= 0) {
System.out.println("The key: " + key + " found at index = " + result);
} else {
// a negative result is -(insertion point) - 1
int insertionPoint = -result - 1;
System.out.println("The key: " + key + " is not found in the array (it would be inserted at index " + insertionPoint + ")");
}
}
public static void main(String[] args) {
int[] array = {50, 80, 10, 40, 60, 30, 70, 20};
Arrays.sort(array); // binarySearch needs a sorted array
System.out.println("Sorted array: " + Arrays.toString(array));
report(array, 60);
report(array, 100);
}
}
Sorted array: [10, 20, 30, 40, 50, 60, 70, 80] The key: 60 found at index = 5 The key: 100 is not found in the array (it would be inserted at index 8)
Time complexity: O(log n), where n is the length of the array.
4. Using Collections.binarySearch()
java.util.Collections.binarySearch() does the same for a sorted List, with the same return-value convention.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// Binary search on a sorted List with Collections.binarySearch()
public class CollectionsBinarySearchExample {
static void report(List<Integer> list, int key) {
int result = Collections.binarySearch(list, key);
if (result >= 0) {
System.out.println("The key: " + key + " found at index = " + result);
} else {
System.out.println("The key: " + key + " is not found in the list");
}
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(List.of(10, 20, 30, 40, 50)); // already sorted
report(list, 30);
report(list, 90);
}
}
The key: 30 found at index = 2 The key: 90 is not found in the list
Time complexity: O(log n) for random-access lists such as ArrayList. For a large LinkedList, which has no random access, it still makes only O(log n) comparisons but needs O(n) link traversals.
Advantages of the Binary Search Algorithm
- O(log n) time: very fast even for huge sorted arrays.
- O(1) extra space in its iterative form.
- Short and simple once the boundary conditions are right; the JDK provides ready-made versions.
- Variants answer related questions in O(log n): the first or last occurrence of a value, the insertion point, or the smallest value meeting a condition.
Disadvantages of the Binary Search Algorithm
- The data must be sorted; sorting first costs O(n log n), which only pays off if you search many times.
- It needs fast random access, so it is not efficient on linked lists.
- Keeping an array sorted while inserting and deleting costs O(n) per change; a balanced search tree or a hash table is better for frequently changing data.
- Off-by-one mistakes in the boundaries are a common source of bugs.