Skip to content

Fix infinite loop in JumpSearch when key exceeds last element - #7555

Merged
alxkm merged 1 commit into
TheAlgorithms:masterfrom
SEPURI-SAI-KRISHNA:fix/jump-search-infinite-loop
Aug 5, 2026
Merged

Fix infinite loop in JumpSearch when key exceeds last element#7555
alxkm merged 1 commit into
TheAlgorithms:masterfrom
SEPURI-SAI-KRISHNA:fix/jump-search-infinite-loop

Conversation

@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor

JumpSearch.find never terminates when the key is greater than the last element of the array.

The jumping loop clamps its cursor to the last index:

while (limit < length && key.compareTo(array[limit]) > 0) {
    limit = Math.min(limit + blockSize, length - 1);   // <-- stops advancing
}

Once limit reaches length - 1, Math.min(limit + blockSize, length - 1) evaluates back to length - 1, so the cursor stops moving. If the key is still greater than array[length - 1], the loop condition stays true forever and the call hangs, burning CPU.

Reproducer:

new JumpSearch().find(new Integer[] {1, 2, 3, 4}, 5); // never returns

The clamp is unnecessary — the loop condition limit < length already stops the scan, and the linear-search loop that follows is bounded by i <= limit && i < length. Letting limit run past the end restores termination without changing any other behaviour.

Fix

limit = Math.min(limit + blockSize, length - 1);limit += blockSize;

Tests

  • testJumpSearchKeyGreaterThanLastElement — the minimal reproducer, with @Timeout(threadMode = SEPARATE_THREAD) so the hang surfaces as a failure instead of stalling CI.
  • testJumpSearchKeyGreaterThanLastElementForEveryLength — same, for lengths 1–50, since the block size depends on the array length.
  • testJumpSearchFindsEveryElement — every index of every array of length 1–50 is still found, covering the elements that sit exactly on a jump boundary.

All three fail (time out) on master and pass with the fix. The existing tests never used a key above the maximum, which is why this went unnoticed.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.41%. Comparing base (ec0f2cd) to head (233a78c).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7555      +/-   ##
============================================
- Coverage     80.42%   80.41%   -0.01%     
+ Complexity     7457     7456       -1     
============================================
  Files           815      815              
  Lines         24055    24055              
  Branches       4732     4732              
============================================
- Hits          19346    19345       -1     
  Misses         3945     3945              
- Partials        764      765       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit 171bdc5 into TheAlgorithms:master Aug 5, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants