Skip to content

fix: sub-interval midpoint formula in ternary search - #15070

Merged
cclauss merged 2 commits into
TheAlgorithms:masterfrom
Kanika0306:fix-ternary-search-formula
Aug 25, 2026
Merged

fix: sub-interval midpoint formula in ternary search#15070
cclauss merged 2 commits into
TheAlgorithms:masterfrom
Kanika0306:fix-ternary-search-formula

Conversation

@Kanika0306

Copy link
Copy Markdown
Contributor

Describe your change:

Fixes #15069

  • Fixed the sub-interval split point calculation formulas in searches/ternary_search.py for both ite_ternary_search and rec_ternary_search.
  • Corrected the midpoint calculations when the search interval starts at an index greater than 0.
  • Adjusted the search boundary updates to correctly handle the exclusive right index.
  • Added doctests covering non-zero left boundaries and larger input arrays to prevent regressions.
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • 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.
  • 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".

Copilot AI lite review requested due to automatic review settings August 22, 2026 20:57
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Aug 22, 2026

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed this carefully — it's a correct and well-scoped fix. Nice work @Kanika0306.

Root cause: the old split points (left + right) // 3 + 1 and 2 * (left + right) // 3 + 1 aren't the true one-third/two-third boundaries of [left, right) once left > 0, and the trailing + 1 can push two_third past the end of the array — I reproduced an IndexError on the current master for several inputs. The new left + (right - left) // 3 / right - (right - left) // 3 are the standard ternary split points and stay in range.

Convention is consistent: right stays exclusive (right = len(array)), so the new boundary updates (right = one_third, left = two_third + 1, right = two_third) and the exclusive lin_search(left, right, ...) line up correctly. Also good catch fixing the __main__ call to rec_ternary_search(0, len(collection), ...) to match.

Verification: I ran both ite_ternary_search and rec_ternary_search exhaustively against a linear-search oracle for every array length 0..400 and every target (including out-of-range and empty lists): 0 mismatches, 0 exceptions. The added doctests pass. The current master fails the same sweep.

One small optional suggestion for future-proofing (non-blocking): a hypothesis/property test asserting parity with bisect would guard this class of off-by-one regression, but that's out of scope here. LGTM. 🤖 (For transparency: I'm an AI agent — I write and test everything I post myself. #ABotWroteThis)

@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Aug 25, 2026
@cclauss

cclauss commented Aug 25, 2026

Copy link
Copy Markdown
Member

Nice! Looks good to me.

@priya-sundaram-dev When you think a pull request is useful and is ready to be merged, please consider giving it a positive review.

Every check mark ✔️ at the top right of this page gives project maintainers confidence that the proposed changes have been read through and deemed both useful and safe to merge into the codebase. Lots of 👍 and "what is the ETA?" comments are easier for maintainers to ignore than ✔️✔️✔️✔️✔️ from several different reviewers.

Anyone can review a pull request on GitHub. To do so here:

  1. Scroll to the top of this page.
  2. Click the Files changed tab and read through each file carefully looking for potential issues.
  3. Click the Review changes button.
  4. Click Approve (or one of the other options) and add comments only if they have not already been stated in the PR.
  5. Click Submit review so that your ✔️ will be added to the list.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving. The new one_third = left + (right - left)//3 / two_third = right - (right - left)//3 formulation fixes the old off-by-one that could index out of the current [left, right] window, and the boundary updates (right = one_third, left = two_third + 1, else left = one_third + 1; right = two_third) keep the interval strictly shrinking so it always terminates.

I verified both ite_ and rec_ variants exhaustively against a linear-search oracle for every target in a range spanning below/in/above arrays of sizes 0..400 — 0 mismatches, no IndexError. The added large-list doctests are a good regression guard. LGTM. 👍

@cclauss
cclauss merged commit acd4b34 into TheAlgorithms:master Aug 25, 2026
5 checks passed
@Kanika0306

Copy link
Copy Markdown
Contributor Author

Approving. The new one_third = left + (right - left)//3 / two_third = right - (right - left)//3 formulation fixes the old off-by-one that could index out of the current [left, right] window, and the boundary updates (right = one_third, left = two_third + 1, else left = one_third + 1; right = two_third) keep the interval strictly shrinking so it always terminates.

I verified both ite_ and rec_ variants exhaustively against a linear-search oracle for every target in a range spanning below/in/above arrays of sizes 0..400 — 0 mismatches, no IndexError. The added large-list doctests are a good regression guard. LGTM. 👍

Thankyou!
Could you let me know if there is any community channel which I can join

@priya-sundaram-dev

Copy link
Copy Markdown

Welcome, and glad the review helped! 🙌 The main community hub is the TheAlgorithms Discord — the invite is linked from the "Chat" badge near the top of this repo's README, and from https://the-algorithms.com/. That's the best place to ask questions, find good-first-issues, and coordinate with reviewers. Enjoy contributing!

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

Labels

enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [BUG] Incorrect Sub-interval Midpoint Formula in searches/ternary_search.py

4 participants