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
20 changes: 11 additions & 9 deletions searches/exponential_search.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env python3

"""
Pure Python implementation of exponential search algorithm
Pure Python implementation of the exponential search algorithm
For more information, see the Wikipedia page:
https://en.wikipedia.org/wiki/Exponential_search
For doctests run the following command:
Run doctests with the following command:
python3 -m doctest -v exponential_search.py
For manual testing run:
For manual testing, run:
python3 exponential_search.py
"""

Expand All @@ -22,10 +22,10 @@ def binary_search_by_recursion(
left: int = 0,
right: int | None = None,
) -> int:
"""Pure implementation of binary search algorithm in Python using recursion
"""Pure implementation of the binary search algorithm in Python using recursion
Be careful: the collection must be ascending sorted otherwise, the result will be
unpredictable.
Be careful: the collection must be ascendingly sorted; otherwise, the result will
be unpredictable.
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item value to search
Expand Down Expand Up @@ -78,14 +78,14 @@ def exponential_search(sorted_collection: list[int], item: int) -> int:
For more information, refer to:
https://en.wikipedia.org/wiki/Exponential_search
Be careful: the collection must be ascending sorted, otherwise the result will be
unpredictable.
Be careful: the collection must be ascendingly sorted; otherwise the result will
be unpredictable.
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item value to search
:return: index of the found item or -1 if the item is not found
The time complexity of this algorithm is O(log i) where i is the index of the item.
The time complexity of this algorithm is O(log i), where i is the index of the item.
Examples:
>>> exponential_search([0, 5, 7, 10, 15], 0)
Expand All @@ -100,6 +100,8 @@ def exponential_search(sorted_collection: list[int], item: int) -> int:
-1
>>> exponential_search([0, 5, 7, 10, 15], 20)
-1
>>> exponential_search([], 1) # Empty array edge case
-1
"""
if list(sorted_collection) != sorted(sorted_collection):
raise ValueError("sorted_collection must be sorted in ascending order")
Expand Down
Loading