Last Updated : 11 Jul, 2025
Try it on GfG Practice
Binary search is a highly efficient searching algorithm used when the input is sorted. It works by repeatedly dividing the search range in half, reducing the number of comparisons needed compared to a linear search. Here, we are focusing on finding the middle element that acts as a reference frame to determine whether to go left or right to it as the elements are already sorted This makes it ideal for large datasets, where it achieves a time complexity of O(log N) far faster than O(N) for sequential search.
Example Input/Output:
Input: arr = { 3, 5, 7, 8, 10, 12, 15}, target = 7
Output: 2Input: arr = { 1, 2, 3, 4, 5, 6, 7, 8} target = 10
Output: -1 ( Because the target is not present in the array)
Key Points:
Below is the Algorithm designed for Binary Search:
Now you must be thinking what if the input is not sorted then the results are undefined.
Importance of Binary SearchNote: If there are duplicates, there is no guarantee which one will be found.
There are three methods in Java to implement Binary Search in Java are mentioned below:
Example: Binary Search program using iterative method.
Java
// Java implementation of iterative Binary Search
class Geeks
{
static int binarySearch(int a[], int l, int r, int x)
{
while (l <= r) {
int m = (l + r) / 2;
// Index of Element Returned
if (a[m] == x) {
return m;
// If element is smaller than mid, then
// it can only be present in left subarray
// so we decrease our r pointer to mid - 1
} else if (a[m] > x) {
r = m - 1;
// Else the element can only be present
// in right subarray
// so we increase our l pointer to mid + 1
} else {
l = m + 1;
}
}
// No Element Found
return -1;
}
public static void main(String args[])
{
int a[] = { 2, 3, 4, 10, 40 };
int n = a.length;
int x = 10;
int res = binarySearch(a, 0, n - 1, x);
System.out.println("Element to be searched is : "+ x);
if (res == -1)
System.out.println("Element is not present in array");
else
System.out.println("Element is present at index: " + res);
}
}
Element to be searched is : 10 Element is present at index: 3
2. Recursive Method for Binary SearchTip: Geeks you must be wondering out whether there is any function like lower_bound() or upper_bound() just likely found in C++ STL. so the straight answer is that there was no function only till Java 9, later onwards they were added.
Example: Binary Search program using recursion.
Java
// Java implementation of
// recursive Binary Search
public class Geeks
{
static int binarySearch(int a[], int l, int r, int x)
{
if (r >= l) {
int m = l + (r - l) / 2;
// Returned Index of the Element
if (a[m] == x)
return m;
// If element is smaller than mid, then
// it can only be present in left subarray
if (a[m] > x)
return binarySearch(a, l, m - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(a, m + 1, r, x);
}
// No Element Found
return -1;
}
// main function
public static void main(String args[])
{
int a[] = { 2, 3, 4, 10, 40 };
int n = a.length;
int x = 10;
int res = binarySearch(a, 0, n - 1, x);
System.out.println("Element to be searched is : "+ x);
if (res == -1)
System.out.println(
"Element is not present in array");
else
System.out.println("Element is present at index: " + res);
}
}
Element to be searched is : 10 Element is present at index: 3Complexity of the above method
3. In Build Method for Binary Search in JavaTime Complexity: O(log N)
Space Complexity: O(1), If the recursive call stack is considered then the auxiliary space will be O(log N)
Arrays.binarysearch() works for arrays which can be of primitive data type also.
Example: Binary Search program in Java using in-build method Arrays.binarysearch().
Java
// Java Program to demonstrate working of binarySearch()
// Method of Arrays class In a sorted array
import java.util.Arrays;
public class Geeks
{
public static void main(String[] args)
{
int a[] = { 10, 20, 15, 22, 35 };
// Sorting the above array
// using sort() method of Arrays class
Arrays.sort(a);
int x = 22;
int res = Arrays.binarySearch(a, x);
System.out.println("Element to be searched is : "+ x);
if (res >= 0)
System.out.println(x + " found at index = " + res);
else
System.out.println(x + " Not found");
x = 40;
res = Arrays.binarySearch(a, x);
System.out.println("Element to be searched is : "+ x);
if (res >= 0)
System.out.println(x + " found at index = " + res);
else
System.out.println(x + " Not found");
}
}
Element to be searched is : 22 22 found at index = 3 Element to be searched is : 40 40 Not foundBinary Search in Java Collections
Now let us see how Collections.binarySearch() work for LinkedList. So basically as discussed above this method runs in log(n) time for a "random access" list like ArrayList. If the specified list does not implement the RandomAccess interface and is large, this method will do an iterator-based binary search that performs O(n) link traversals and O(log n) element comparisons.
Collections.binarysearch() works for objects Collections like ArrayList and LinkedList.
Example: Binary Search using Collection.binarysearch() on arraylist and linkedlist.
Java
// Java Program to Demonstrate Working of binarySearch()
// method of Collections class
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Geeks
{
public static void main(String[] args)
{
List<Integer> a = new ArrayList<Integer>();
// Populating the Arraylist
a.add(1);
a.add(2);
a.add(3);
a.add(10);
a.add(20);
int x = 10;
int res = Collections.binarySearch(a, x);
System.out.println("Element to be searched is : "+ x);
if (res >= 0)
System.out.println(x + " found at index = " + res);
else
System.out.println(x + " Not found");
x = 15;
res = Collections.binarySearch(a, x);
if (res >= 0)
System.out.println(x + " found at index = " + res);
else
System.out.println(x + " Not found");
}
}
Element to be searched is : 10 10 found at index = 3 15 Not found
The complexity of the above method:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4