Home Page

Binary Search Algorithm

The Binary Search Algorithm (BSA) is a method for finding an element in a sorted array. It uses the divide-and-conquer principle, repeatedly splitting the search space in half by comparing the target element with the middle element until the target is found or the search space is empty. It is also known as half-interval search.

Prerequisites for Binary Search

Step-by-Step Algorithm

Key → Target element

Mid → Middle index of the search space

How to Calculate the Middle Index

Not Recommended

mid = (left index + right index) / 2

If the left index or right index is a very large value (close to the maximum integer limit), then their sum (left index + right index) can overflow, leading to incorrect results or runtime errors. This issue is common in languages like C, C++, and Java, where integers have fixed-size limits.

Recommended

mid = left + (right - left) / 2

right index – left index is always a small and safe number. It prevents integer overflow produces the same correct middle index

Binary Search Algorithm

Step by step visualization of binary search algorithm

Binary Search Visualization

Binary Search Implementations

The binary search algorithm can be implemented by using the two methods given below:

Iterative Binary Search Algorithm: It involves a loop to keep dividing the search space in half until the target value is found or the search space is empty.

binary search algorithm -  Iterative Appraoch

Recursive Binary Search Algorithm: It finds a target element in the searching space by splitting it into two halves and recursively calling itself until it finds the target element.

binary search algorithm -  Recursive Appraoch

Time Complexity of Binary Search

The binary search algorithm divides the search space into two halves until the key element is found or the search space becomes empty. Due to this divide-and-conquer principle, binary search has a logarithmic time complexity. That’s why it is also called a logarithmic search

Space Complexity of Binary Search

Space complexity differs slightly depending on the implementation.

Drawbacks of Binary Search

Requires a Sorted Array
Binary search works only on sorted data. If the array is unsorted, it must be sorted first, which itself takes O(n log n) time—sometimes making binary search inefficient for one-time searches.

Not Suitable for Linked Lists
Binary search requires random access to elements (direct indexing). Since linked lists do not support random access, binary search is inefficient or impractical for such data structures.

Overhead for Small Datasets
For small arrays, the overhead of calculating midpoints and maintaining indices may make binary search slower than linear search, which is simpler and faster in such cases.

Data Must Remain Static
If elements are frequently inserted or deleted, maintaining the sorted order becomes costly. In such dynamic datasets, binary search may not be the best choice.

Recursive Implementation Uses Extra Space
The recursive version of binary search requires O(log n) extra space due to the recursion call stack, making it less space-efficient than the iterative approach.

References

#BinarySearch #Algorithms #DataStructures #Python #DSA