Binary Search | Python
Binary Search is an efficient searching algorithm used to find an element in a sorted array by repeatedly dividing the search interval in half. It reduces the time complexity to O(log N), making it much faster than linear search.
How Binary Search Works
- Find the middle element of the array.
- Compare the middle element with the target key.
- If equal: return index.
- If the key is smaller: search the left half.
- If the key is larger: search the right half.
- Repeat until the element is found or the search space is empty.
Binary Search using the bisect
Python provides the bisect module, which offers built-in functions to maintain a list in sorted order and perform efficient binary searches internally.
import bisect
def binary_search_bisect(arr, x):
i = bisect.bisect_left(arr, x)
if i != len(arr) and arr[i] == x:
return i
else:
return -1
arr = [2, 3, 4, 10, 40]
x = 10
result = binary_search_bisect(arr, x)
if result != -1:
print("Element is present at index", result)
else:
print("Element is not present in array")
Output
Element is present at index 3
Explanation:
- bisect_left(arr, x): finds the position where x should be inserted to keep arr sorted.
- Checks if x exists at that position; if yes, returns the index.
- Returns -1 if the element is not found.
Iterative Binary Search
This version manually performs binary search using a while loop - efficient in both time and space.
def binary_search(arr, x):
low = 0
high = len(arr) - 1
while low <= high:
mid = low + (high - low) // 2
if arr[mid] < x:
low = mid + 1
elif arr[mid] > x:
high = mid - 1
else:
return mid
return -1
arr = [2, 3, 4, 10, 40]
x = 10
result = binary_search(arr, x)
if result != -1:
print("Element is present at index", result)
else:
print("Element is not present in array")
Output
Element is present at index 3
Explanation:
- Using a while loop to implement iterative binary search .
- Initialize low and high pointers to define the search range.
- Calculates the middle index mid = low + (high - low) // 2.
- If arr[mid] is less than x, moves search to the right half.
- If arr[mid] is greater than x, moves search to the left half.
- If arr[mid] == x, returns the index.
- Returns -1 if the element is not found.
Recursive Binary Search
Recursion simplifies the logic but increases memory usage due to function call stacks.
def binary_search(arr, low, high, x):
if high >= low:
mid = low + (high - low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
arr = [2, 3, 4, 10, 40]
x = 10
result = binary_search(arr, 0, len(arr) - 1, x)
if result != -1:
print("Element is present at index", result)
else:
print("Element is not present in array")
Output
Element is present at index 3
Explanation:
- Using recursion to implement binary search on a sorted list.
- Calculates the middle index and compares its value with x.
- If x matches the middle element, returns its index.
- If x is smaller, searches the left half; if larger, searches the right half.
- Returns -1 if the element is not found.