fix: sub-interval midpoint formula in ternary search - #15070
Conversation
for more information, see https://pre-commit.ci
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
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)
|
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:
|
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
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! |
|
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! |
Describe your change:
Fixes #15069
searches/ternary_search.pyfor bothite_ternary_searchandrec_ternary_search.0.rightindex.leftboundaries and larger input arrays to prevent regressions.Checklist: