88public class BucketSortHashBehaviorTest {
99
1010 private static <T extends Comparable <T >> int pseudoHash (final T element , final T min , final T max , final int numberOfBuckets ) {
11- //Reproduces the production hash() logic
11+ // Reproduces the production hash() logic
1212 double range = max .compareTo (min );
1313 double normalizedValue = element .compareTo (min ) / range ; // -1/0/1 divided by -1/0/1
1414 return (int ) (normalizedValue * (numberOfBuckets - 1 ));
1515 }
1616
17- @ Test //Test case when all numbers are equal
17+ @ Test // Test case when all numbers are equal
1818 void sortStillCorrectWhenAllEqual () {
1919 Integer [] arr = {1 , 1 , 1 , 1 , 1 };
2020 Integer [] expected = arr .clone ();
2121
2222 new BucketSort ().sort (arr );
2323 assertArrayEquals (expected , arr );
2424
25- //Observe bucket mapping (all collapse to index 0)
25+ // Observe bucket mapping (all collapse to index 0)
2626 Integer min = 1 , max = 1 ;
2727 int numberOfBuckets = Math .max (arr .length / 10 , 1 ); // same as BUCKET_DIVISOR rule
2828 int idx = pseudoHash (1 , min , max , numberOfBuckets );
2929 // idx will be 0 because NaN cast to int -> 0 in Java
3030 System .out .println ("All-equal case -> bucket index: " + idx );
3131 }
3232
33- @ Test //Test case with non-equal integers
33+ @ Test // Test case with non-equal integers
3434 void sortStillCorrectNonEqualIntegers () {
3535 Integer [] arr = {20 , 40 , 30 , 10 };
3636 Integer [] expected = {10 , 20 , 30 , 40 };
@@ -47,10 +47,10 @@ void sortStillCorrectNonEqualIntegers() {
4747 int idx = pseudoHash (x , min , max , numberOfBuckets );
4848 System .out .println ("Value " + x + " -> bucket " + idx );
4949 }
50- //Expect only two distinct buckets because compareTo gives -1/0/1
50+ // Expect only two distinct buckets because compareTo gives -1/0/1
5151 }
5252
53- @ Test //Test case when the Array contains Strings
53+ @ Test // Test case when the Array contains Strings
5454 void sortStillCorrectWhenStrings () {
5555 String [] arr = {"apple" , "banana" , "carrot" };
5656 String [] expected = arr .clone ();
0 commit comments