Skip to content

fixed logical error in parameter "hi" the bisect functions of the bin… - #13900

Open
Nisarg-patel-2410 wants to merge 2 commits into
TheAlgorithms:masterfrom
Nisarg-patel-2410:master
Open

fixed logical error in parameter "hi" the bisect functions of the bin…#13900
Nisarg-patel-2410 wants to merge 2 commits into
TheAlgorithms:masterfrom
Nisarg-patel-2410:master

Conversation

@Nisarg-patel-2410

@Nisarg-patel-2410 Nisarg-patel-2410 commented Nov 12, 2025

Copy link
Copy Markdown

Describe your change:

This update improves how the binary search function handles negative values passed to hi. Earlier, when hi was negative, the code reset it to len(sorted_collection), which ignored Python’s negative-index behavior.

The new logic adjusts hi by converting the negative index into its proper positive position. This makes the function behave more consistently with Python slicing rules.

What Changed

Added support for negative indexing in the hi parameter:

if hi < 0: hi = len(sorted_collection) + hi + 1

This ensures the search range is interpreted correctly when callers use negative indices.

  • 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 #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Nov 12, 2025
Comment thread searches/binary_search.py
"""
if hi < 0:
hi = len(sorted_collection)
hi = len(sorted_collection) + hi + 1 # in case of negetive indexing used for hi

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good fix for negative indexing support. However there's a typo in
the inline comment — "negetive" should be "negative":

in case of negative indexing used for hi

Comment thread searches/binary_search.py
"""
if hi < 0:
hi = len(sorted_collection)
hi = len(sorted_collection) + hi + 1 # in case of negetive indexing used for hi

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Worth adding a doctest that demonstrates the negative index behavior
to verify this fix works as expected and prevent regressions:

binary_search([1, 2, 3, 4, 5], 4, 0, -1)
3

This would make the edge case explicitly testable.

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for looking at this, but I think the new formula isn't quite right and needs a bit more work before it can go in.

The change makes hi < 0 mean "negative index from the end" via hi = len(sorted_collection) + hi + 1. Testing that against normal negative-index semantics, it's off by one: for sorted_collection = [0, 5, 7, 10, 15], hi = -1 yields hi = 5 (the whole array), whereas negative indexing would make -1 refer to the last element (hi = 4). So -1 and the old hi default now behave identically, and the intended "stop before the last element" case isn't reachable.

A few asks before this can be approved:

  • Decide and document the exact semantics of a negative hi (the docstring says this mirrors bisect.bisect_left, and CPython's bisect doesn't special-case negative hi at all — worth confirming the desired behaviour).
  • Add doctests that exercise negative hi for both bisect_left and bisect_right, since the current examples don't cover it and CI can't catch a regression here.
  • Minor: typo in the comment, "negetive" → "negative".

Happy to re-review once the semantics are pinned down and covered by a doctest. 🙏

@cclauss cclauss added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes A maintainer has requested changes to this PR enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants