镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Incorrect output for Binary Search when list contains duplicates #13840

Closed as not planned

Description

@anumolusneha

Repository commit

3929955ab0649cddff28864bfa5ed8f506b48847

Python version (python --version)

Python 3.12.3

Dependencies version (pip freeze)

attrs==23.2.0
Babel==2.10.3
bcc==0.29.1
blinker==1.7.0
Brlapi==0.8.5
certifi==2023.11.17
cfgv==3.4.0
chardet==5.2.0
click==8.1.6
cloud-init==25.1.4
colorama==0.4.6
command-not-found==0.3
configobj==5.0.8
cryptography==41.0.7
cupshelpers==1.0
dbus-python==1.3.2
defer==1.0.6
distlib==0.4.0
distro==1.9.0
distro-info==1.7+build1
filelock==3.20.0
httplib2==0.20.4
identify==2.6.15
idna==3.6
iniconfig==2.3.0
Jinja2==3.1.2
jsonpatch==1.32
jsonpointer==2.0
jsonschema==4.10.3
language-selector==0.1
launchpadlib==1.11.0
lazr.restfulclient==0.14.6
lazr.uri==1.0.6
louis==3.29.0
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
netaddr==0.8.0
netifaces==0.11.0
nodeenv==1.9.1
oauthlib==3.2.2
olefile==0.46
packaging==25.0
pexpect==4.9.0
pillow==10.2.0
pip==24.0
platformdirs==4.5.0
pluggy==1.6.0
pre_commit==4.3.0
ptyprocess==0.7.0
pycairo==1.25.1
pycups==2.0.1
Pygments==2.17.2
PyGObject==3.48.2
PyJWT==2.7.0
pyparsing==3.1.1
pyrsistent==0.20.0
pyserial==3.5
pytest==8.4.2
python-apt==2.7.7+ubuntu4
python-dateutil==2.8.2
python-debian==0.1.49+ubuntu2
pytz==2024.1
pyxdg==0.28
PyYAML==6.0.1
requests==2.31.0
rich==13.7.1
ruff==0.14.3
setuptools==68.1.2
six==1.16.0
ssh-import-id==5.11
systemd-python==235
typing_extensions==4.10.0
ubuntu-drivers-common==0.0.0
ubuntu-pro-client==8001
ufw==0.36.2
unattended-upgrades==0.1
urllib3==2.0.7
virtualenv==20.35.4
wadllib==1.3.6
wheel==0.42.0
xdg==5
xkit==0.0.0

Expected behavior

Binary search should return the index of the first matching element when duplicates exist.

Actual behavior

The algorithm returns the last index of the matching element instead of the first.

Activity

  1. added a commit that references this issue on Nov 2, 2025
    24b7987
  2. added a commit that references this issue on Nov 5, 2025
    57d417d
  3. Aditya-Singh27 commented on Nov 7, 2025

    @Aditya-Singh27

    I'll like to work on this issue , please assign it to me

  4. poorva-gupta commented on Nov 30, 2025

    @poorva-gupta

    hi, can you please assign this issue to me.

  5. sneha-sharma17 commented on Dec 19, 2025

    @sneha-sharma17

    Hi , I'd like to work on this issue.

    I tested the current binary search implementation and confirmed that it returns
    the last index when duplicates exist.

    Before I proceed, can I confirm:

    • Should binary search always return the first occurrence,
      or
    • Should we add a separate function like binary_search_first()?

    If approved, I will implement the fix along with test coverage.
    Thanks!

  6. added a commit that references this issue on Jan 19, 2026
    de9d3b2
  7. tusharynayaka commented on Feb 28, 2026

    @tusharynayaka

    Hi! I'd like to work on this bug. Could you please assign this issue to me? Thanks!

  8. added a commit that references this issue on Mar 2, 2026
    3dcff79
  9. Yashang-Parmar commented on Jun 4, 2026

    @Yashang-Parmar

    Hi! I'd love to solve this binary search duplicate bug. Could you please assign this issue to me?

  10. mazhorin-coder commented on Jun 5, 2026

    @mazhorin-coder

    Hi, I'd like to help with this issue.

  11. cclauss commented on Sep 10, 2026

    @cclauss
    Member

    Not a defect. binary_search([1,2,2,2,3], 2) returns 2, which is a valid index of the target. Classic binary search guarantees an index of a matching element, not the leftmost one. Closing as working as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions