Skip to content

Commit 848f3c6

Browse files
Merge branch 'master' into fix/factorial-biginteger-overflow
2 parents 5609777 + 635d54a commit 848f3c6

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/main/java/com/thealgorithms/searches/BinarySearch.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,18 @@ class BinarySearch implements SearchAlgorithm {
5858
*/
5959
@Override
6060
public <T extends Comparable<T>> int find(T[] array, T key) {
61-
// Handle edge case: empty array
61+
// Handle edge case: null or empty array
6262
if (array == null || array.length == 0) {
6363
return -1;
6464
}
6565

66+
// Handle edge case: null key
67+
// Searching for null in an array of Comparables is undefined behavior
68+
// Return -1 to indicate not found rather than throwing NPE
69+
if (key == null) {
70+
return -1;
71+
}
72+
6673
// Delegate to the core search implementation
6774
return search(array, key, 0, array.length - 1);
6875
}

0 commit comments

Comments
 (0)