Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions searches/exponential_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,29 @@ def binary_search_by_recursion(
>>> binary_search_by_recursion([0, 5, 7, 10, 15], 16)
-1
"""
if right is None:
right = len(sorted_collection) - 1
if list(sorted_collection) != sorted(sorted_collection):
raise ValueError("sorted_collection must be sorted in ascending order")
if right < left:
return -1
if right is None:
right = len(sorted_collection) - 1

midpoint = left + (right - left) // 2
# Recursive core: ``left`` and ``right`` are always concrete indices here, so the
# window can only shrink. Keeping the recursion flat (no ``None`` sentinel, no
# re-expansion) is what prevents the runaway recursion when the item sits below
# ``sorted_collection[0]`` and ``right`` legitimately drops below ``left``.
def _search(left: int, right: int) -> int:
if right < left:
return -1

if sorted_collection[midpoint] == item:
return midpoint
elif sorted_collection[midpoint] > item:
return binary_search_by_recursion(sorted_collection, item, left, midpoint - 1)
else:
return binary_search_by_recursion(sorted_collection, item, midpoint + 1, right)
midpoint = left + (right - left) // 2

if sorted_collection[midpoint] == item:
return midpoint
elif sorted_collection[midpoint] > item:
return _search(left, midpoint - 1)
else:
return _search(midpoint + 1, right)

return _search(left, right)


def exponential_search(sorted_collection: list[int], item: int) -> int:
Expand Down
Loading