Skip to content

Fix binary search final#14523

Open
tusharynayaka wants to merge 15 commits intoTheAlgorithms:masterfrom
tusharynayaka:fix-binary-search-final
Open

Fix binary search final#14523
tusharynayaka wants to merge 15 commits intoTheAlgorithms:masterfrom
tusharynayaka:fix-binary-search-final

Conversation

@tusharynayaka
Copy link
Copy Markdown

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.

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant