Python
Python copied to clipboard
Fix: incorrect output for Binary Search with duplicate elements #13840
Fix incorrect output for Binary Search when list contains duplicates (Fixes #13840)
Describe your change:
Fixed a bug in the binary search algorithm that caused incorrect output when the list contained duplicate elements.
- Updated the condition to correctly return the index of the first matching element.
- Ensured the loop continues searching the left half when a duplicate is found.
- Verified behavior with lists containing multiple identical values.
- [ ] Add an algorithm?
- [x] Fix a bug or typo in an existing algorithm?
- [ ] Add or change doctests?
- [ ] Documentation change?
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 filenames are in all lowercase characters with no spaces or dashes.
- [x] All function 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.
- [ ] All new algorithms include at least one URL that points to Wikipedia or another similar explanation. (Not applicable here)
- [x] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #13840".
Hi! Thanks for reviewing. Please let me know if any changes are needed. I’ve followed the contributing guidelines and added doctests. Looking forward to feedback!