Introduction
Binary Search means:
- efficiently searching
inside sorted array
by repeatedly dividing range
Goal:
- reduce search space
by half each step
Requirement:
Array must be sorted Example:
Array:[1, 3, 5, 7, 9, 11]
Target:
7
Output:
Index 3
Explanation:
Middle element matches target value,so search stops. This problem is one of the most important applications of:
Divide and Conquer Constraints
1 <= Array Size <= 10^5Approach : Iterative Binary Search
Explanations:
Explanation:
The idea is:
- compare target
with middle element - discard half array
each iteration
Steps:
- Initialize left pointer.
- Initialize right pointer.
- Find middle index.
- Compare middle value.
- Move left or right range.
- Repeat until found.
Conditions:
target == middle → return index target < middle → move lefttarget > middle → move right This approach:
- uses iterative traversal
- reduces search complexity significantly
Dry Run
Array:
[1, 3, 5, 7, 9, 11]
Target:
7
Middle:
5
7 > 5
Move right.
Middle:
7Target found.
Practice :