The Wayback Machine - https://web.archive.org/web/20260228093033/https://github.com/TheAlgorithms/Python/pull/13946
Skip to content

[Searches] Fix Binary Search bug with duplicate elements#13946

Merged
poyea merged 6 commits intoTheAlgorithms:masterfrom
shuhao-alan-fan:fix-merge-sort-empty-list-issue#13901
Dec 13, 2025
Merged

[Searches] Fix Binary Search bug with duplicate elements#13946
poyea merged 6 commits intoTheAlgorithms:masterfrom
shuhao-alan-fan:fix-merge-sort-empty-list-issue#13901

Conversation

@shuhao-alan-fan
Copy link
Contributor

@shuhao-alan-fan shuhao-alan-fan commented Nov 22, 2025

Describe your change:

This PR fixes a bug where the Binary Search algorithm returned incorrect results (or failed) when the input list contained duplicate elements.

I have updated the logic to handle duplicates correctly and added doctests to verify the fix.

Fixes #13886

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • 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.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes Binary Search gives wrong output for duplicate elements #13886".
@shuhao-alan-fan
Copy link
Contributor Author

Hi maintainers, this PR fixes issue #13886 and all tests + pre-commit checks are passing.
Whenever you have time, I would appreciate a review.

Thank you!

@shuhao-alan-fan shuhao-alan-fan changed the title Fixes #13886 Dec 12, 2025
@poyea poyea requested a review from Copilot December 12, 2025 16:54
@poyea poyea force-pushed the fix-merge-sort-empty-list-issue#13901 branch from 37fe148 to ad1574f Compare December 12, 2025 16:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new binary_search_with_duplicates function to handle binary search in sorted lists containing duplicate elements. The implementation uses lower_bound and upper_bound helper functions to find the range of all occurrences of the target value, addressing issue #13886.

Key changes:

  • Adds binary_search_with_duplicates function that returns a list of all indices where the target occurs
  • Implements lower_bound and upper_bound as nested helper functions using binary search
  • Includes doctests covering single occurrences, multiple duplicates, and not-found cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

shuhao-alan-fan and others added 2 commits December 12, 2025 17:38
Added docstrings for lower_bound and upper_bound functions.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Dec 13, 2025
Updated docstring parameter and return type annotations for lower_bound and upper_bound functions.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Dec 13, 2025
@poyea poyea merged commit 2c15b8c into TheAlgorithms:master Dec 13, 2025
5 checks passed
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Dec 13, 2025
thotaDeepika added a commit to thotaDeepika/Python that referenced this pull request Dec 16, 2025
[Searches] Fix Binary Search bug with duplicate elements (TheAlgorithms#13946)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants