Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Binary Search (bisect) in Python
The bisect module in Python provides functions for binary search operations on sorted lists. Binary search is an efficient algorithm that finds elements in O(log n) time complexity, making it much faster than linear search for large datasets.
We will explore three common binary search operations using the bisect module ?
Finding First Occurrence of an Element
The bisect_left() function returns the leftmost insertion point for a value in a sorted list. This helps us find the first occurrence of an element ?
Syntax
bisect.bisect_left(a, x, lo=0, hi=len(a))
Parameters:
-
a- sorted list -
x- element to search -
lo- start index (optional) -
hi- end index (optional)
Example
from bisect import bisect_left
def find_first_occurrence(arr, target):
i = bisect_left(arr, target)
if i != len(arr) and arr[i] == target:
return i
else:
return -1
numbers = [2, 3, 4, 4, 5, 8, 12, 36, 36, 36, 85, 89, 96]
target = 4
pos = find_first_occurrence(numbers, target)
if pos == -1:
print(f"{target} is absent")
else:
print(f"First occurrence of {target} is at position {pos}")
First occurrence of 4 is at position 2
Finding Greatest Value Smaller Than Target
Using bisect_left(), we can find the largest element that is smaller than our target value ?
Example
from bisect import bisect_left
def find_largest_smaller(arr, target):
i = bisect_left(arr, target)
if i:
return i - 1
else:
return -1
numbers = [2, 3, 4, 4, 5, 8, 12, 36, 36, 36, 85, 89, 96]
target = 8
pos = find_largest_smaller(numbers, target)
if pos == -1:
print(f"No element smaller than {target}")
else:
print(f"Largest value smaller than {target} is {numbers[pos]} at position {pos}")
Largest value smaller than 8 is 5 at position 4
Finding Last Occurrence of an Element
The bisect_right() function returns the rightmost insertion point, helping us find the last occurrence of an element ?
Example
from bisect import bisect_right
def find_last_occurrence(arr, target):
i = bisect_right(arr, target)
if i != 0 and arr[i-1] == target:
return i - 1
else:
return -1
numbers = [2, 3, 4, 4, 5, 8, 12, 36, 36, 36, 85, 89, 96]
target = 36
pos = find_last_occurrence(numbers, target)
if pos == -1:
print(f"{target} is absent")
else:
print(f"Last occurrence of {target} is at position {pos}")
Last occurrence of 36 is at position 9
Comparison of bisect Functions
| Function | Purpose | Returns |
|---|---|---|
bisect_left() |
Find leftmost insertion point | Index where element should be inserted |
bisect_right() |
Find rightmost insertion point | Index after last occurrence |
Conclusion
The bisect module provides efficient binary search operations with O(log n) time complexity. Use bisect_left() for finding first occurrences and bisect_right() for finding last occurrences in sorted lists.
