diff --git a/searches/binary_search.py b/searches/binary_search.py index 2e66b672d5b4..e0ffd3e31216 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -198,20 +198,12 @@ def binary_search(sorted_collection: list[int], item: int) -> int: >>> binary_search([0, 5, 7, 10, 15], 6) -1 """ - if list(sorted_collection) != sorted(sorted_collection): - raise ValueError("sorted_collection must be sorted in ascending order") - left = 0 - right = len(sorted_collection) - 1 - - while left <= right: - midpoint = left + (right - left) // 2 - current_item = sorted_collection[midpoint] - if current_item == item: - return midpoint - elif item < current_item: - right = midpoint - 1 - else: - left = midpoint + 1 + # Use bisect_left to find the leftmost position + index = bisect_left(sorted_collection, item) + + # Check if the item exists at that position + if index != len(sorted_collection) and sorted_collection[index] == item: + return index return -1