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/recursive_binary_search.md
+27-24Lines changed: 27 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ description: "In this blog post, we'll explore the recursive binary search algor
7
7
tags: [dsa, algorithms, binary search, recursive]
8
8
---
9
9
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.
11
11
12
12
## What is Recursive Binary Search?
13
13
@@ -44,22 +44,26 @@ int recursiveBinarySearch(int array[], int low, int high, int key) {
44
44
}
45
45
```
46
46
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.
51
53
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.
58
64
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
61
66
62
-
Practical Applications
63
67
Iterative binary search is widely used in various real-world applications and algorithmic problems:
64
68
65
69
1. Searching in a Sorted Array
@@ -99,25 +103,24 @@ Upper Bound Pseudo-Code:
99
103
100
104
```cpp
101
105
FUNCTION upperBound(array, low, high, key):
102
-
IF low == high:
103
-
RETURN low
106
+
if low == high:
107
+
return low
104
108
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)
111
113
112
114
```
113
115
114
116
2. Rotated Sorted Array
115
117
Recursive binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point.
116
118
117
-
Tips for Implementing Recursive Binary Search
119
+
:::Tips
118
120
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty.
119
121
Prevent Stack Overflow: Be mindful of the recursion depth, especially for large arrays, as deep recursion can lead to stack overflow.
120
122
Efficiency: The recursive approach can be more intuitive and elegant, but consider the iterative approach for environments with limited stack size.
123
+
:::
121
124
122
-
In Conclusion
125
+
##Conclusion
123
126
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