You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/dsa/binary_search/Iterative_binary_search.md
+17-11Lines changed: 17 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,20 +48,25 @@ int iterativeBinarySearch(int array[], int size, int key) {
48
48
}
49
49
```
50
50
51
-
How Iterative Binary Search Works
52
-
Step-by-Step Explanation
53
-
Initialize: Set two pointers, low at the beginning and high at the end of the array.
54
-
Middle Element: Calculate the middle element's index.
51
+
## How Iterative Binary Search Works
52
+
53
+
### Step-by-Step Explanation
54
+
55
+
1. Initialize: Set two pointers, low at the beginning and high at the end of the array.
56
+
2. Middle Element: Calculate the middle element's index.
55
57
Comparison:
56
-
If the middle element is the target, return its index.
57
-
If the middle element is less than the target, discard the left half by setting low to mid + 1.
58
-
If the middle element is greater than the target, discard the right half by setting high to mid - 1.
59
-
Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer.
60
-
Time Complexity
58
+
3. If the middle element is the target, return its index.
59
+
4. If the middle element is less than the target, discard the left half by setting low to mid + 1.
60
+
5. If the middle element is greater than the target, discard the right half by setting high to mid - 1.
61
+
6. Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer.
62
+
63
+
### Time Complexity
64
+
61
65
The time complexity of iterative binary search is $O(logn)$,
62
66
where $𝑛$ is the number of elements in the array. This logarithmic time complexity makes iterative binary search significantly faster than linear search for large datasets.
63
67
64
-
Practical Applications
68
+
## Practical Applications
69
+
65
70
Iterative binary search is widely used in various real-world applications and algorithmic problems:
66
71
67
72
1. Searching in a Sorted Array
@@ -118,10 +123,11 @@ FUNCTION upperBound(array, key):
118
123
2. Rotated Sorted Array
119
124
Iterative binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point.
120
125
121
-
Tips for Implementing Iterative Binary Search
126
+
:::tip
122
127
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty.
123
128
Prevent Overflow: When calculating the middle index, use $\text{mid} = \text{low} + \frac{\text{high} - \text{low}}{2}$ instead of $\text{mid} = \frac{\text{low} + \text{high}}{2}$ to prevent potential overflow.
124
129
Efficiency: The iterative approach often uses less memory than the recursive approach because it doesn't involve the overhead of multiple recursive function calls.
0 commit comments