Iterative Binary Search
Iterative Binary Search is powerful algorithm that is essential for efficiently finding elements in sorted arrays, making it a staple in the toolkit of any adept programmer. Whether you're optimizing search operations or solving complex algorithmic challenges, understanding iterative binary search is crucial. Let's delve into its mechanics, applications, and implementation.
What is Iterative Binary Search?
Iterative binary search is a highly efficient algorithm used to find an element in a sorted array. It works by repeatedly dividing the search interval in half, using an iterative approach. If the value of the search key is less than the item in the middle of the interval, the algorithm narrows the interval to the lower half. Otherwise, it narrows it to the upper half. The process continues until the search key is found or the interval is empty.
Video Explanation

In pseudo-code, iterative binary search is defined as follows:
FUNCTION iterativeBinarySearch(array, key):
low = 0
high = array.length - 1
WHILE low <= high:
mid = (low + high) / 2
IF array[mid] == key:
RETURN mid
ELSE IF array[mid] < key:
low = mid + 1
ELSE:
high = mid - 1
RETURN -1
int iterativeBinarySearch(int array[], int size, int key) {
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == key) {
return mid;
} else if (array[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
How Iterative Binary Search Works
Step-by-Step Explanation
- Initialize: Set two pointers, low at the beginning and high at the end of the array.
- Middle Element: Calculate the middle element's index. Comparison:
- If the middle element is the target, return its index.
- If the middle element is less than the target, discard the left half by setting low to mid + 1.
- If the middle element is greater than the target, discard the right half by setting high to mid - 1.
- Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer.
Time Complexity
The time complexity of iterative binary search is , where