Skip to content

Fix exponential_search recursing forever when the item is below the first element - #15384

Open
Darkslayer3324j wants to merge 1 commit into
TheAlgorithms:masterfrom
Darkslayer3324j:fix/exponential-search-recursion
Open

Darkslayer3324j wants to merge 1 commit into
TheAlgorithms:masterfrom
Darkslayer3324j:fix/exponential-search-recursion

Conversation

@Darkslayer3324j

Copy link
Copy Markdown
Contributor

Describe your change

  • 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.

exponential_search (and binary_search_by_recursion) never terminate when the item is smaller than every element:

>>> exponential_search([0, 5, 7, 10, 15], -3)
RecursionError: maximum recursion depth exceeded
>>> binary_search_by_recursion([0, 5, 7, 10, 15], -1)
RecursionError: maximum recursion depth exceeded

binary_search_by_recursion uses right=-1 to mean "not given" (if right < 0: right = len(...) - 1). When the item is below the first element the recursion legitimately reaches right = midpoint - 1 = -1, which is then mistaken for "not given", right is reset to the last index, and the search starts over forever. The fix makes the sentinel None, which cannot collide with a real index. Nothing else in the repository calls this function.

Doctests for an item below and above the range are added. I also compared both functions with a membership check on 30,000 random sorted lists (lengths 0-15, values -8..8): 0 mismatches after the 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". (No open issue for this.)

I left "all my own work" unticked on purpose: this change was written with AI assistance (Claude Code, as AGENTS.md invites). I found the problem by fuzzing the functions in searches/ against a membership check, reproduced it, and ran the doctests, ruff check and ruff format on the changed file.

…irst element

binary_search_by_recursion used right=-1 as its 'not given' sentinel, but the
recursion legitimately reaches right=-1 when the item is smaller than every
element, which reset right to len-1 and never terminated. Use None as the
sentinel and add doctests for items below and above the range.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Sep 20, 2026
@oga35767-eng

Copy link
Copy Markdown

👍

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

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants