diff --git a/searches/binary_search.py b/searches/binary_search.py index 5125dc6bdb9a..80fc0880bf3b 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -198,8 +198,11 @@ 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") + if any( + sorted_collection[i] > sorted_collection[i + 1] + for i in range(len(sorted_collection) - 1) +): + raise ValueError("sorted_collection must be sorted in ascending order") left = 0 right = len(sorted_collection) - 1