9
9
public class BucketSortHashBehaviorTest {
10
10
11
11
private static <T extends Comparable <T >> int pseudoHash (final T element , final T min , final T max , final int numberOfBuckets ) {
12
- //Reproduces the production hash() logic
12
+ // Reproduces the production hash() logic
13
13
double range = max .compareTo (min );
14
14
double normalizedValue = element .compareTo (min ) / range ; // -1/0/1 divided by -1/0/1
15
15
return (int ) (normalizedValue * (numberOfBuckets - 1 ));
16
16
}
17
17
18
- @ Test //Test case when all numbers are equal
18
+ @ Test // Test case when all numbers are equal
19
19
void sort_stillCorrect_whenAllEqual () {
20
20
Integer [] arr = {1 , 1 , 1 , 1 , 1 };
21
21
Integer [] expected = arr .clone ();
22
22
23
23
new BucketSort ().sort (arr );
24
24
assertArrayEquals (expected , arr );
25
25
26
- //Observe bucket mapping (all collapse to index 0)
26
+ // Observe bucket mapping (all collapse to index 0)
27
27
Integer min = 1 , max = 1 ;
28
28
int numberOfBuckets = Math .max (arr .length / 10 , 1 ); // same as BUCKET_DIVISOR rule
29
29
int idx = pseudoHash (1 , min , max , numberOfBuckets );
30
- //idx will be 0 because NaN cast to int -> 0 in Java
30
+ // idx will be 0 because NaN cast to int -> 0 in Java
31
31
System .out .println ("All-equal case -> bucket index: " + idx );
32
32
}
33
33
34
- @ Test //Test case with non-equal integers
34
+ @ Test // Test case with non-equal integers
35
35
void sort_stillCorrect_nonEqualIntegers () {
36
36
Integer [] arr = {20 , 40 , 30 , 10 };
37
37
Integer [] expected = {10 , 20 , 30 , 40 };
@@ -51,7 +51,7 @@ void sort_stillCorrect_nonEqualIntegers() {
51
51
// Expect only two distinct buckets because compareTo gives -1/0/1
52
52
}
53
53
54
- @ Test //Test case when the Array contains Strings
54
+ @ Test // Test case when the Array contains Strings
55
55
void sort_stillCorrect_whenStrings () {
56
56
String [] arr = {"apple" , "banana" , "carrot" };
57
57
String [] expected = arr .clone ();
@@ -70,4 +70,3 @@ void sort_stillCorrect_whenStrings() {
70
70
// Buckets reflect only lexicographic order, not a numeric spacing
71
71
}
72
72
}
73
-
0 commit comments