Fix incorrect output in Binary Search when duplicates exist(Fixes #13840)#14322
Closed
tusharynayaka wants to merge 1 commit intoTheAlgorithms:masterfrom
Closed
Fix incorrect output in Binary Search when duplicates exist(Fixes #13840)#14322tusharynayaka wants to merge 1 commit intoTheAlgorithms:masterfrom
tusharynayaka wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
Closing this pull request as invalid@tusharynayaka, this pull request is being closed as the files submitted contains an invalid extension. This repository only accepts Python algorithms. Please read the Contributing guidelines first. Invalid files in this pull request: |
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 of Change
Updated the binary search algorithm to return the first occurrence of an item when duplicates exist in the collection, rather than returning any random occurrence.
Added a check to see if the element immediately to the left of the found midpoint is also the target item.
If it is, the search continues to the left side to find the true first index.
Updated doctests to verify this behavior.
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 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] 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