## DEV Community

Marc Peejay V. Viernes

Posted on

# Mastering Binary Search in Python 🚀

Introduction:
Welcome back to another exciting blog post! Today, we're diving into the fascinating world of algorithms, particularly focusing on a classic and efficient search algorithm known as Binary Search. We'll discuss what binary search is, how it works, and why it's so powerful. Moreover, we'll walk through a Python implementation of binary search for a sorted array, solving a common problem along the way.

## Binary Search: What's the Buzz About? 🎯

Binary search is a fundamental algorithm used to efficiently locate a target value within a sorted array. It's called "binary" because it continually divides the search range in half until the target value is found or determined to be absent. This results in a logarithmic runtime complexity, denoted as O(log n), making it incredibly fast for large datasets.

Time Complexity: O(log n)

Space Complexity: O(1)

### The Problem at Hand: Searching in a Sorted Array 🔍

Consider a scenario where we have an array of integers sorted in ascending order, and we need to find a particular target value within it. If the target exists in the array, we want to return its index; otherwise, we return -1. This problem aligns perfectly with the capabilities of binary search.

### The Solution: Python Implementation 🐍

Let's dive into the Python implementation of the binary search algorithm to solve the aforementioned problem. Here's a breakdown of the solution:

``````def search(nums: List[int], target: int) -> int:
l, r = 0, len(nums) - 1
while l <= r:
m = l + ((r - l) // 2)  # Calculate the middle index to avoid overflow
if nums[m] > target:
r = m - 1  # Adjust the search range to the left half
elif nums[m] < target:
l = m + 1  # Adjust the search range to the right half
else:
return m  # Target found, return its index

``````

### Explanation of the Solution with Complexity Analysis

• We initialize two pointers, `l` (left) and `r` (right), which represent the current search range within the array.
• In each iteration of the while loop, we calculate the middle index `m` of the current search range. We ensure to avoid overflow by using the formula `(l + (r - l) // 2)`.
• We compare the value at index `m` with the target value:
• If `nums[m]` is greater than the target, we adjust the search range to the left half by updating `r` to `m - 1`.
• If `nums[m]` is less than the target, we adjust the search range to the right half by updating `l` to `m + 1`.
• If `nums[m]` equals the target, we've found our answer and return the index `m`.
• If the target is not found after exhausting all possibilities, we return -1.

Complexity Analysis:

Let n be the size of the input array nums.

• Time complexity: O(log n)

• nums is divided into half each time. In the worst-case scenario, we need to cut nums until the range has no element, it takes logarithmic time to reach this break condition.
• Space complexity: O(1)

• During the loop, we only need to record three indexes, left, right, and mid, they take constant space.

### Applications of Binary Search 🌐

• Searching: As demonstrated in our problem, binary search efficiently locates an element in a sorted array.
• Finding Peaks: In a mountain-like array, binary search can find the peak element efficiently.
• Efficient Range Queries: Binary search can be used to perform efficient range queries, such as finding the first or last occurrence of a particular element in a sorted list.

Conclusion:
Binary search is a powerful algorithm for searching within sorted arrays, offering logarithmic runtime complexity. In this blog post, we've explored the concept of binary search, discussed its efficiency, provided a Python implementation, and highlighted some of its applications. With this knowledge, you're now equipped to leverage binary search in your own projects efficiently. Happy coding!

For further exploration and practice, visit the "Binary Search" problem on LeetCode. Happy coding! 🚀