Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
style: apply clang-format per CI
  • Loading branch information
Leogricci committed Aug 29, 2025
commit e21a21fa669430d4ab34a20512a960430f99e4f8
26 changes: 13 additions & 13 deletions src/main/java/com/thealgorithms/sorts/BucketSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ private <T extends Comparable<T>> T[] concatenateBuckets(Iterable<List<T>> bucke
*
*<p><b>Important limitations:</b>
*<ul>
* <li>This method uses {@code compareTo} as if it provided a numeric difference.
* For numeric types, {@code compareTo} only reports order (−1, 0, 1), not the actual distance.
* This often collapses distribution into one or two buckets.</li>
* <li>For non-numeric {@code Comparable} types (for example {@code String}), bucket indices depend on lexicographic
* code-point differences, which are not a proportional measure of spacing. Distribution is therefore arbitrary and uneven.</li>
* <li>If {@code min.equals(max)}, the computed "range" is 0. Then {@code element.compareTo(min) / 0}
* yields {@code NaN}, which Java coerces to 0 when cast to {@code int}.
* Practically, all elements collapse into bucket 0 in this case.</li>
* </ul>
*
* <p>Despite these limitations, the sort remains correct because each bucket is sorted internally and concatenated.
* This method should be regarded as a simplified demonstration rather than a
* general-purpose bucketing strategy for arbitrary {@code Comparable<T>} values.</p>
* <li>This method uses {@code compareTo} as if it provided a numeric difference.
* For numeric types, {@code compareTo} only reports order (−1, 0, 1), not the actual distance.
* This often collapses distribution into one or two buckets.</li>
* <li>For non-numeric {@code Comparable} types (for example {@code String}), bucket indices depend on lexicographic
* code-point differences, which are not a proportional measure of spacing. Distribution is therefore arbitrary and uneven.</li>
* <li>If {@code min.equals(max)}, the computed "range" is 0. Then {@code element.compareTo(min) / 0}
* yields {@code NaN}, which Java coerces to 0 when cast to {@code int}.
* Practically, all elements collapse into bucket 0 in this case.</li>
* </ul>
*
* <p>Despite these limitations, the sort remains correct because each bucket is sorted internally and concatenated.
* This method should be regarded as a simplified demonstration rather than a
* general-purpose bucketing strategy for arbitrary {@code Comparable<T>} values.</p>
*
* @param element the element of the array
* @param min the minimum value in the array
Expand Down
Copy link
Collaborator

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

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.

Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
public class BucketSortHashBehaviorTest {

private static <T extends Comparable<T>> int pseudoHash(final T element, final T min, final T max, final int numberOfBuckets) {
//Reproduces the production hash() logic
// Reproduces the production hash() logic
double range = max.compareTo(min);
double normalizedValue = element.compareTo(min) / range; // -1/0/1 divided by -1/0/1
return (int) (normalizedValue * (numberOfBuckets - 1));
}

@Test //Test case when all numbers are equal
@Test // Test case when all numbers are equal
void sort_stillCorrect_whenAllEqual() {
Integer[] arr = {1, 1, 1, 1, 1};
Integer[] expected = arr.clone();

new BucketSort().sort(arr);
assertArrayEquals(expected, arr);

//Observe bucket mapping (all collapse to index 0)
// Observe bucket mapping (all collapse to index 0)
Integer min = 1, max = 1;
int numberOfBuckets = Math.max(arr.length / 10, 1); // same as BUCKET_DIVISOR rule
int idx = pseudoHash(1, min, max, numberOfBuckets);
//idx will be 0 because NaN cast to int -> 0 in Java
// idx will be 0 because NaN cast to int -> 0 in Java
System.out.println("All-equal case -> bucket index: " + idx);
}

@Test //Test case with non-equal integers
@Test // Test case with non-equal integers
void sort_stillCorrect_nonEqualIntegers() {
Integer[] arr = {20, 40, 30, 10};
Integer[] expected = {10, 20, 30, 40};
Expand All @@ -51,7 +51,7 @@ void sort_stillCorrect_nonEqualIntegers() {
// Expect only two distinct buckets because compareTo gives -1/0/1
}

@Test //Test case when the Array contains Strings
@Test // Test case when the Array contains Strings
void sort_stillCorrect_whenStrings() {
String[] arr = {"apple", "banana", "carrot"};
String[] expected = arr.clone();
Expand All @@ -70,4 +70,3 @@ void sort_stillCorrect_whenStrings() {
// Buckets reflect only lexicographic order, not a numeric spacing
}
}

Loading