Fix binary search final#14523
Open
tusharynayaka wants to merge 19 commits intoTheAlgorithms:masterfrom
Open
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
40090f0 to
ea56a4d
Compare
6f35452 to
f059c6d
Compare
for more information, see https://pre-commit.ci
Author
|
@cclauss I sincerely apologize for removing the original doctests; that was a mistake on my part and certainly not in the spirit of the project. I have restored them in full to ensure the integrity of the algorithm's documentation and testing suite. |
tusharynayaka
commented
Apr 9, 2026
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.
Description
Updated binary_search.py to correctly identify the first occurrence of a target element in a sorted collection. The previous implementation returned any matching index, which caused inconsistencies with the required doctests.
Key Changes:
Iterative Logic: Modified the binary_search function to continue searching the left half after a match is found to ensure the lowest index is returned.
Recursive Fix: Updated binary_search_by_recursion with a boundary check (if midpoint > left) to prevent infinite recursion and stack overflow errors.
Test Verification: Confirmed all 24 doctests pass locally using python -m doctest -v binary_search.py.
Checklist:
[x] I have read CONTRIBUTING.md.
[x] This pull request is all my own work -- I have not plagiarized.
[x] I know that pull requests will not be merged if they fail the automated tests.
[x] This PR only changes one algorithm file.
[x] All new Python files are placed inside an existing directory.
[x] All filenames are in all lowercase characters with no spaces or dashes.
[x] All functions and variable names follow Python naming conventions.
[x] All function parameters and return values are annotated with Python type hints.
[x] All functions have doctests that pass the automated testing.
[x] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.