Skip to content

Commit 5666842

Browse files
committed
Fix DoubleHashingSort: add NullPointerException for null values
- Add explicit null value check in sort() method before processing - Throw NullPointerException when null values found in array (as expected by tests) - Remove null handling from RobustComparator since nulls are rejected upfront - Fix JavaDoc formatting issues (trailing spaces) - Ensures shouldHandleArrayWithNullValues and shouldHandleListWithNullValues tests pass Fixes: ✓ shouldHandleArrayWithNullValues: now throws expected NullPointerException ✓ shouldHandleListWithNullValues: now throws expected NullPointerException ✓ Clang-format compliance: removed trailing spaces in JavaDoc comments
1 parent b3909d6 commit 5666842

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/main/java/com/thealgorithms/sorts/DoubleHashingSort.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
2424
return array;
2525
}
2626

27+
// Check for null values in the array - tests expect NullPointerException
28+
for (T element : array) {
29+
if (element == null) {
30+
throw new NullPointerException("Array contains null values");
31+
}
32+
}
33+
2734
// Use a robust comparator that handles all edge cases
2835
Arrays.sort(array, new RobustComparator<>());
2936
return array;
@@ -33,14 +40,12 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
3340
* A robust comparator that handles edge cases for different data types
3441
*/
3542
private static class RobustComparator<T extends Comparable<T>> implements Comparator<T> {
36-
43+
3744
@Override
3845
@SuppressWarnings("unchecked")
3946
public int compare(T a, T b) {
40-
// Handle null values
41-
if (a == null && b == null) return 0;
42-
if (a == null) return -1;
43-
if (b == null) return 1;
47+
// Note: nulls are already checked and rejected in sort() method
48+
// so we don't need null handling here
4449

4550
// Handle floating point numbers with special values
4651
if (a instanceof Double && b instanceof Double) {

src/test/java/com/thealgorithms/sorts/DoubleHashingSortTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* Test class for DoubleHashingSort algorithm
5-
*
5+
*
66
* Tests the DoubleHashingSort implementation against the standard
77
* SortingAlgorithmTest suite which includes edge cases for:
88
* - Negative numbers

0 commit comments

Comments
 (0)