8
8
public class BucketSortHashBehaviorTest {
9
9
10
10
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
12
12
double range = max .compareTo (min );
13
13
double normalizedValue = element .compareTo (min ) / range ; // -1/0/1 divided by -1/0/1
14
14
return (int ) (normalizedValue * (numberOfBuckets - 1 ));
15
15
}
16
16
17
- @ Test //Test case when all numbers are equal
17
+ @ Test // Test case when all numbers are equal
18
18
void sortStillCorrectWhenAllEqual () {
19
19
Integer [] arr = {1 , 1 , 1 , 1 , 1 };
20
20
Integer [] expected = arr .clone ();
21
21
22
22
new BucketSort ().sort (arr );
23
23
assertArrayEquals (expected , arr );
24
24
25
- //Observe bucket mapping (all collapse to index 0)
25
+ // Observe bucket mapping (all collapse to index 0)
26
26
Integer min = 1 , max = 1 ;
27
27
int numberOfBuckets = Math .max (arr .length / 10 , 1 ); // same as BUCKET_DIVISOR rule
28
28
int idx = pseudoHash (1 , min , max , numberOfBuckets );
29
29
// idx will be 0 because NaN cast to int -> 0 in Java
30
30
System .out .println ("All-equal case -> bucket index: " + idx );
31
31
}
32
32
33
- @ Test //Test case with non-equal integers
33
+ @ Test // Test case with non-equal integers
34
34
void sortStillCorrectNonEqualIntegers () {
35
35
Integer [] arr = {20 , 40 , 30 , 10 };
36
36
Integer [] expected = {10 , 20 , 30 , 40 };
@@ -47,10 +47,10 @@ void sortStillCorrectNonEqualIntegers() {
47
47
int idx = pseudoHash (x , min , max , numberOfBuckets );
48
48
System .out .println ("Value " + x + " -> bucket " + idx );
49
49
}
50
- //Expect only two distinct buckets because compareTo gives -1/0/1
50
+ // Expect only two distinct buckets because compareTo gives -1/0/1
51
51
}
52
52
53
- @ Test //Test case when the Array contains Strings
53
+ @ Test // Test case when the Array contains Strings
54
54
void sortStillCorrectWhenStrings () {
55
55
String [] arr = {"apple" , "banana" , "carrot" };
56
56
String [] expected = arr .clone ();
0 commit comments