binary_search: fix O(n log n) validation, remove redundant checks, add generics#14454
Closed
Mias007 wants to merge 2 commits intoTheAlgorithms:masterfrom
Closed
binary_search: fix O(n log n) validation, remove redundant checks, add generics#14454Mias007 wants to merge 2 commits intoTheAlgorithms:masterfrom
Mias007 wants to merge 2 commits intoTheAlgorithms:masterfrom
Conversation
Closing this pull request as invalid@Mias007, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines. If you're facing any problem on how to mark a checkbox, please read the following instructions:
NOTE: Only |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Fix O(n log n) sort validation — binary_search_std_lib and binary_search_by_recursion used list(col) != sorted(col), which allocates a full copy and sorts it on every call. Replaced with an O(n) adjacent-pair check via pairwise (already used correctly in binary_search). Extracted into a shared _check_sorted() helper to avoid further duplication.
Remove per-frame validation in recursion — binary_search_by_recursion ran the sort check on every recursive call. Validation now happens once in the public entry-point; recursion is delegated to a private _binary_search_recursive() helper.
Remove redundant inner functions — binary_search_with_duplicates defined its own lower_bound/upper_bound functions that were functionally identical to the module-level bisect_left/bisect_right. Removed and replaced with calls to the existing functions.
Remove dead None-guard in exponential_search — if last_result is None: return -1 was unreachable because the callee never returns None. Also updated exponential_search to call _binary_search_recursive directly, skipping redundant re-validation on an already-checked collection.
Widen types from list[int] to Sequence[T] — All public functions now accept any Sequence of comparable items, not just list[int]. Mirrors the real bisect module's signature.
Add all — Declares the public API explicitly.
Add empty-collection edge-case tests — binary_search([], 1), binary_search_by_recursion([], 1), exponential_search([], 1) each return -1 correctly and are now covered by doctests.