Skip to content

Commit 6dc95df

Browse files
authored
Update Iterative_binary_search.md
1 parent efee848 commit 6dc95df

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

docs/dsa/binary_search/Iterative_binary_search.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,25 @@ int iterativeBinarySearch(int array[], int size, int key) {
4848
}
4949
```
5050

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.
5557
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+
6165
The time complexity of iterative binary search is $O(logn)$,
6266
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.
6367

64-
Practical Applications
68+
## Practical Applications
69+
6570
Iterative binary search is widely used in various real-world applications and algorithmic problems:
6671

6772
1. Searching in a Sorted Array
@@ -118,10 +123,11 @@ FUNCTION upperBound(array, key):
118123
2. Rotated Sorted Array
119124
Iterative binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point.
120125

121-
Tips for Implementing Iterative Binary Search
126+
:::tip
122127
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty.
123128
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.
124129
Efficiency: The iterative approach often uses less memory than the recursive approach because it doesn't involve the overhead of multiple recursive function calls.
130+
:::
125131

126132
## Conclusion
127133

0 commit comments

Comments
 (0)