Skip to content

Fix binary search final#14523

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

Fix binary search final#14523
tusharynayaka wants to merge 19 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
@tusharynayaka tusharynayaka force-pushed the fix-binary-search-final branch from 40090f0 to ea56a4d Compare April 9, 2026 10:50
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 9, 2026
@tusharynayaka tusharynayaka force-pushed the fix-binary-search-final branch from 6f35452 to f059c6d Compare April 9, 2026 10:58
@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Apr 9, 2026
@tusharynayaka
Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Author

@tusharynayaka tusharynayaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant