Skip to content

Commit 9ae92b6

Browse files
authored
Update binary_search.md
1 parent 4326dc4 commit 9ae92b6

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

docs/dsa/binary_search/binary_search.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: "In this blog post, we'll dive into the binary search algorithm, a
77
tags: [dsa, algorithms, binary search]
88
---
99

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

1212
## What is Binary Search?
1313

@@ -50,8 +50,9 @@ return -1;
5050
}
5151
```
5252

53-
How Binary Search Works
54-
Step-by-Step Explanation
53+
## How Binary Search Works
54+
55+
### Step-by-Step Explanation
5556

5657
1. Initialize: Set two pointers, low at the beginning and high at the end of the array.
5758
2. Middle Element: Calculate the middle element's index.
@@ -61,13 +62,14 @@ Step-by-Step Explanation
6162
If the middle element is greater than the target, discard the right half by setting high to mid - 1.
6263
4. Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer.
6364

64-
Time Complexity
65-
The time complexity of binary search is
66-
𝑂(log𝑛)
67-
O(logn), where 𝑛
68-
n is the number of elements in the array. This logarithmic time complexity makes binary search significantly faster than linear search for large datasets.
65+
### Time Complexity
66+
67+
The time complexity of binary search is $𝑂(log𝑛)$
68+
69+
where $n$ is the number of elements in the array. This logarithmic time complexity makes binary search significantly faster than linear search for large datasets.
70+
71+
## Practical Applications
6972

70-
Practical Applications
7173
Binary search is widely used in various real-world applications and algorithmic problems:
7274

7375
1. Searching in a Sorted Array
@@ -121,11 +123,11 @@ RETURN low
121123
2. Rotated Sorted Array
122124
Binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point.
123125

124-
Tips for Implementing Binary Search
125-
126+
:::Tip
126127
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty.
127128
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.
128129
Iterative vs. Recursive: Both iterative and recursive implementations are valid. Choose based on your preference and the problem constraints.
130+
:::
129131

130-
In Conclusion
131-
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 binary search works and how to implement its variations, you'll be well-equipped to tackle numerous challenges in your programming journey. Happy coding!
132+
## Conclusion
133+
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 binary search works and how to implement its variations, you'll be well-equipped to tackle numerous challenges in your programming journey.

0 commit comments

Comments
 (0)