Skip to content

binary_search: fix O(n log n) validation, remove redundant checks, add generics#14454

Closed
Mias007 wants to merge 2 commits intoTheAlgorithms:masterfrom
Mias007:improve-binary-search-doc
Closed

binary_search: fix O(n log n) validation, remove redundant checks, add generics#14454
Mias007 wants to merge 2 commits intoTheAlgorithms:masterfrom
Mias007:improve-binary-search-doc

Conversation

@Mias007
Copy link
Copy Markdown

@Mias007 Mias007 commented Mar 26, 2026

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.

@algorithms-keeper
Copy link
Copy Markdown

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:

  • Read a point one at a time and think if it is relevant to the pull request or not.
  • If it is, then mark it by putting a x between the square bracket like so: [x]

NOTE: Only [x] is supported so if you have put any other letter or symbol between the brackets, that will be marked as invalid. If that is the case then please open a new pull request with the appropriate changes.

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed invalid labels Mar 26, 2026
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant