[CS Fundamentals] Binary Search
![[CS Fundamentals] Binary Search](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1747041878054%2Fc8d8adb9-097d-484b-9562-f43e4bce9a46.png&w=3840&q=75)
Computer Science Enthusiast with a Drive for Excellence | Data Science | Web Development | Passionate About Tech & Innovation
Idea
Binary Search is a powerful algorithm to search for a certain value. If you were to find 3 in an array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], how are you going to come up with the index of 3? The first method that pops up in your head would be looking at each value starting from index 0 to the end, and see if there are any matching values. However, this method is inefficient if you have a large amount of data.
We can use binary search to make it more efficient. In binary search, we set left, right and mid. If a value of mid index is greater than the target value, we divide an array with mid becoming the left.
For example, our target is 3, and mid is 4. In [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], arr[4] = 4, so it is greater than the target, so we only look at the left side of the array like this: [0, 1, 2, 3]. If the target was 7, the target is greater than arr[mid], so we look at the right side of the array like this: [5, 6, 7, 8, 9] becasue it is guaranteed that the target is not on the other half.
Code
Using Recursion:
def binary_search(arr, target, left, right):
if left > right:
return None
mid = (left + right) // 2
if array[mid] == target:
return mid
elif array[mid] > target:
return binary_search(arr, target, left, right - 1)
else:
return binary_search(arr, target, left + 1, right)
Using While Loop:
def binary_search(arr, target, left, right):
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
right = mid - 1
else:
left = mid + 1
return None
Python Bisect Library
Bisect is a python library that helps you to easily do a binary search with a single line of code. You can implement binary search with the code above, but you can save your time with bisect.
bisect_left(literable, value): returns the left-most element that can be insrted while keeping the list sorted.
bisect_right(literable, value): returns the right-most element that can be insrted while keeping the list sorted.
To help you understand, I have a picutre below.

from bisect import bisect_left, bisect_right
a = [1, 2, 4, 4, 8]
x = 4
print(bisect_left(a, x)) # 2
print(bisect_right(a, x)) # 4
Using bisect, we can calculate the number of elements on a certain boundary.
from bisect import bisect_left, bisect_right
def count_by_range(a, left, right):
right_idx = bisect_right(a, right)
left_idx = bisect_left(a, left)
return right_idx - left_idx
a = [1, 2, 3, 3, 3, 3, 4, 4, 8, 9]
# returns the number of 4 in list
print(count_by_range(a, 4, 4)) # 2
# returns the number of elements in a range of -1 to 3.
print(count_by_range(a, -1, 3)) # 6
![[CS fundamentals] Linked List Creation & Insertion explained in C.](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6a74e5c0-44ca-4110-9ee7-e8e67767a9dc.png&w=3840&q=75)
![[CS Fundamentals] Pointers in C](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6f81f5a6-d87c-4382-883f-fa4d0a8b5968.png&w=3840&q=75)
![[SQL] Writing Efficient SQL Queries](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1751292603168%2Fb0b43c7d-8e08-47b2-a64c-b600e3511831.jpeg&w=3840&q=75)
![[CS Fundamentals] Sorting Algorithms](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1746973590876%2Ffab37371-1496-42ef-950b-432e7637c89e.png&w=3840&q=75)