Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Document hash() limitations in BucketSort; add tests showing distribution behavior #6511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Leogricci wants to merge 8 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from Leogricci:docs/hash-warning

Conversation

Copy link

@Leogricci Leogricci commented Aug 29, 2025

Relates to #6503
distribution behavior

Context
Documents the behavior and limitations of hash() in BucketSort. The current logic treats compareTo as if it returned a numeric distance. For numerics, compareTo yields only -1/0/1 (order), and for non-numeric types (e.g., String) the magnitude reflects lexicographic code-point differences rather than proportional spacing. Sorting remains correct due to per-bucket sorting and concatenation, but the resulting bucket distribution is not a meaningful proportional mapping.

Changes

  • Expanded Javadoc on hash() explaining limitations and the degenerate min == max case.
  • Added BucketSortHashBehaviorTest to demonstrate:
    • All equal values collapse to bucket 0.
    • Numeric arrays map mostly to two buckets (because compareTo is sign-only).
    • Strings distribute by lexicographic differences (arbitrary/uneven spacing).

Observed output (examples)
All-equal case -> bucket index: 0
Value 10 -> bucket 0
Value 20 -> bucket 3
Value 30 -> bucket 3
Value 40 -> bucket 3
Value "apple" -> bucket 0
Value "banana" -> bucket 1
Value "carrot" -> bucket 3

Notes
No algorithm/signature change; documentation + tests only (for learners).

  • 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 code is formatted with clang-format -i --style=file path/to/your/file.java

Copy link

codecov-commenter commented Aug 29, 2025
edited
Loading

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.11%. Comparing base (79dc0b4) to head (635fcdb).

Additional details and impacted files
@@ Coverage Diff @@
## master #6511 +/- ##
============================================
+ Coverage 75.10% 75.11% +0.01% 
 Complexity 5571 5571 
============================================
 Files 685 685 
 Lines 19307 19307 
 Branches 3732 3732 
============================================
+ Hits 14500 14502 +2 
+ Misses 4245 4244 -1 
+ Partials 562 561 -1 

☔ View full report in Codecov by Sentry.
📢 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.

@Leogricci Leogricci marked this pull request as ready for review August 29, 2025 02:37
Copy link
Author

Took a while to get the clang-format right but it should be good now.

Copy link
Collaborator

@DenizAltunkapan DenizAltunkapan Aug 29, 2025

Choose a reason for hiding this comment

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

Is there any reason why you didn't put your Tests in the exististing BucketSortTest ? @Leogricci

Copy link
Author

@Leogricci Leogricci Aug 29, 2025

Choose a reason for hiding this comment

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

Since the issue only related to the particular case of the hash function i thought it might be better to create another test class, but I realize now it just makes it harder to find. Sorry for the inconvenience, I will modify it now.

DenizAltunkapan reacted with thumbs up emoji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@DenizAltunkapan DenizAltunkapan DenizAltunkapan left review comments

@yanglbme yanglbme Awaiting requested review from yanglbme yanglbme is a code owner

@vil02 vil02 Awaiting requested review from vil02

@alxkm alxkm Awaiting requested review from alxkm alxkm is a code owner

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /