Skip to content

fix(sorts): stop SwapSort looping forever on duplicate values - #7660

Merged
alxkm merged 2 commits into
TheAlgorithms:masterfrom
Rishavbhattarai:fix/swapsort-duplicates
Oct 27, 2026
Merged

alxkm merged 2 commits into
TheAlgorithms:masterfrom
Rishavbhattarai:fix/swapsort-duplicates

Conversation

@Rishavbhattarai

@Rishavbhattarai Rishavbhattarai commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

SwapSort never finishes when the slot it swaps into already holds an equal value. {2, 2, 1, 1, 0, 0, 0} loops forever: the two 2s keep trading places.

The fix skips past slots that already hold an equal element before swapping, the same way cycle sort handles duplicates. The copies of array[index] belong in the slots starting at index + count. array[index] is one of those copies, so at least one of those slots holds a different value, and the loop stops there.

Added a regression test with a 1 second timeout. It times out on master and passes with the fix.

  • 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. (No new algorithm.)
  • All new algorithms include a corresponding test class that validates their functionality. (Regression test added.)
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.22%. Comparing base (12f69b3) to head (326537a).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7660      +/-   ##
============================================
- Coverage     82.23%   82.22%   -0.01%     
+ Complexity     8399     8398       -1     
============================================
  Files           843      843              
  Lines         26134    26138       +4     
  Branches       5097     5098       +1     
============================================
+ Hits          21490    21492       +2     
  Misses         3853     3853              
- Partials        791      793       +2     

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

@Rishavbhattarai
Rishavbhattarai marked this pull request as ready for review October 27, 2026 03:09

@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 6f19dd4 into TheAlgorithms:master Oct 27, 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