Skip to content

Commit 4326dc4

Browse files
authored
Update recursive_binary_search.md
1 parent 526d829 commit 4326dc4

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

docs/dsa/binary_search/recursive_binary_search.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: "In this blog post, we'll explore the recursive binary search algor
77
tags: [dsa, algorithms, binary search, recursive]
88
---
99

10-
Welcome, eager learner! Today, we embark on an enlightening journey through the world of recursive binary search. This powerful algorithm 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 recursive binary search is crucial. Let's delve into its mechanics, applications, and implementation.
10+
Recursive Binary Search algorithm 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 recursive binary search is crucial. Let's delve into its mechanics, applications, and implementation.
1111

1212
## What is Recursive Binary Search?
1313

@@ -44,22 +44,26 @@ int recursiveBinarySearch(int array[], int low, int high, int key) {
4444
}
4545
```
4646

47-
How Recursive Binary Search Works
48-
Step-by-Step Explanation
49-
Initialize: Set two pointers, low at the beginning and high at the end of the array.
50-
Middle Element: Calculate the middle element's index.
47+
## How Recursive Binary Search Works
48+
49+
### Step-by-Step Explanation
50+
51+
1. Initialize: Set two pointers, low at the beginning and high at the end of the array.
52+
2. Middle Element: Calculate the middle element's index.
5153
Comparison:
52-
If the middle element is the target, return its index.
53-
If the middle element is less than the target, discard the left half by setting low to mid + 1.
54-
If the middle element is greater than the target, discard the right half by setting high to mid - 1.
55-
Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer.
56-
Time Complexity
57-
The time complexity of iterative binary search is 𝑂(log𝑛)
54+
3. If the middle element is the target, return its index.
55+
4. If the middle element is less than the target, discard the left half by setting low to mid + 1.
56+
5. If the middle element is greater than the target, discard the right half by setting high to mid - 1.
57+
6. Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer.
58+
59+
### Time Complexity
60+
61+
The time complexity of iterative binary search is $𝑂(log𝑛)$.
62+
63+
where $n$ is the number of elements in the array. This logarithmic time complexity makes iterative binary search significantly faster than linear search for large datasets.
5864

59-
O(logn), where 𝑛
60-
n is the number of elements in the array. This logarithmic time complexity makes iterative binary search significantly faster than linear search for large datasets.
65+
## Practical Applications
6166

62-
Practical Applications
6367
Iterative binary search is widely used in various real-world applications and algorithmic problems:
6468

6569
1. Searching in a Sorted Array
@@ -99,25 +103,24 @@ Upper Bound Pseudo-Code:
99103
100104
```cpp
101105
FUNCTION upperBound(array, low, high, key):
102-
IF low == high:
103-
RETURN low
106+
if low == high:
107+
return low
104108
mid = (low + high) / 2
105-
IF array[mid] <= key:
106-
RETURN upperBound(array, mid + 1, high, key)
107-
ELSE:
108-
RETURN upperBound(array, low, mid, key)
109-
110-
109+
if array[mid] <= key:
110+
return upperBound(array, mid + 1, high, key)
111+
else:
112+
return upperBound(array, low, mid, key)
111113
112114
```
113115

114116
2. Rotated Sorted Array
115117
Recursive binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point.
116118

117-
Tips for Implementing Recursive Binary Search
119+
:::Tips
118120
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty.
119121
Prevent Stack Overflow: Be mindful of the recursion depth, especially for large arrays, as deep recursion can lead to stack overflow.
120122
Efficiency: The recursive approach can be more intuitive and elegant, but consider the iterative approach for environments with limited stack size.
123+
:::
121124

122-
In Conclusion
125+
## Conclusion
123126
Recursive binary search is a fundamental algorithm that every programmer should master. Its efficiency and versatility make it a powerful tool for solving a wide range of problems. By understanding how recursive binary search works and how to implement its variations, you'll be well-equipped to tackle numerous challenges in your programming journey. Happy coding!

0 commit comments

Comments
 (0)