add exponential search algorithm#10732
Conversation
for more information, see https://pre-commit.ci
… tests delete exponential_search.py file
cclauss
left a comment
There was a problem hiding this comment.
Nice addition!
For optional extra credit, add a timeit benchmark to show relative performance.
searches/binary_search.py
Outdated
There was a problem hiding this comment.
I am not a fan of functions that return multiple datatypes. If the search fails, let's return -1 (like "Hello".find("Z")) or raise a ValueError (like "Hello".index("Z")).
| def exponential_search(sorted_collection: list[int], item: int) -> int | None: | |
| def exponential_search(sorted_collection: list[int], item: int) -> int: |
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
for more information, see https://pre-commit.ci
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
|
I thought more about sorting the sorted collection… it is a bad idea. The second item in might be different in the two lists so the function might tell me that is the second item when in my list it is the third item. So, the implementations should raise ValueError on unsorted input. |
cclauss
left a comment
There was a problem hiding this comment.
This is a really slick addition. Thanks for doing it!
|
@cclauss thank you for your suggestions :) |
|
@cclauss Since exponential search is a different algorithm from binary search, why was it placed in the |
| bound *= 2 | ||
| left = bound // 2 | ||
| right = min(bound, len(sorted_collection) - 1) | ||
| last_result = binary_search_by_recursion( |
There was a problem hiding this comment.
@tianyizheng02 exponential_search() embeds a call to binary_search_by_recursion() but as the benchmarks demonstrate delivers a faster result. Looking at the first commit makes this clearer.
Describe your change:
added exponential algorithm using binary_search_recursion also tests and docs about it
Checklist: