diff --git a/src/main/java/com/thealgorithms/searches/BinarySearch.java b/src/main/java/com/thealgorithms/searches/BinarySearch.java index ca873fc6eafa..4ddfc3cb4ece 100644 --- a/src/main/java/com/thealgorithms/searches/BinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/BinarySearch.java @@ -51,9 +51,9 @@ class BinarySearch implements SearchAlgorithm { * // notFound will be -1 (element 4 does not exist) * * - * @param The type of elements in the array (must be Comparable) + * @param The type of elements in the array (must be Comparable) * @param array The sorted array to search in (MUST be sorted in ascending order) - * @param key The element to search for + * @param key The element to search for * @return The index of the key if found, -1 if not found or if array is null/empty */ @Override @@ -89,10 +89,10 @@ public > int find(T[] array, T key) { *

Time Complexity: O(log n) because we halve the search space each time. * Space Complexity: O(log n) due to recursive call stack. * - * @param The type of elements (must be Comparable) + * @param The type of elements (must be Comparable) * @param array The sorted array to search in - * @param key The element we're looking for - * @param left The leftmost index of current search range (inclusive) + * @param key The element we're looking for + * @param left The leftmost index of current search range (inclusive) * @param right The rightmost index of current search range (inclusive) * @return The index where key is located, or -1 if not found */ @@ -132,3 +132,5 @@ else if (comp < 0) { } } } + + diff --git a/src/main/java/com/thealgorithms/searches/IterativeBinarySearch.java b/src/main/java/com/thealgorithms/searches/IterativeBinarySearch.java index cc0bfb16d26c..3700ca7c81e6 100644 --- a/src/main/java/com/thealgorithms/searches/IterativeBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/IterativeBinarySearch.java @@ -27,13 +27,15 @@ public final class IterativeBinarySearch implements SearchAlgorithm { * Performs iterative binary search on a sorted array. * * @param array the sorted array - * @param key the element to search - * @param type of elements (must be Comparable) + * @param key the element to search + * @param type of elements (must be Comparable) * @return index of the key if found, otherwise -1 */ @Override public > int find(T[] array, T key) { - if (array == null || array.length == 0) { + // Handle edge cases: null/empty array or null key + // Prevents NullPointerException during comparison + if (array == null || array.length == 0 || key == null) { return -1; } @@ -41,6 +43,8 @@ public > int find(T[] array, T key) { int right = array.length - 1; while (left <= right) { + // Use unsigned right shift to avoid overflow + // Equivalent to (left + right) / 2 but safer for large indices int mid = (left + right) >>> 1; int cmp = key.compareTo(array[mid]); @@ -53,6 +57,7 @@ public > int find(T[] array, T key) { } } + // Element not found return -1; } } diff --git a/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java index b2e121ac1ba0..e460c219ba76 100644 --- a/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java @@ -114,4 +114,23 @@ void testBinarySearchLargeArrayNotFound() { Integer key = 9999; // Key not present assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array."); } + + /** + * Test for binary search with a null key. + */ + @Test + void testBinarySearchNullKey() { + IterativeBinarySearch binarySearch = new IterativeBinarySearch(); + Integer[] array = {1, 2, 4, 8, 16}; + assertEquals(-1, binarySearch.find(array, null), "A null key should return -1."); + } + + /** + * Test for binary search with a null array. + */ + @Test + void testBinarySearchNullArray() { + IterativeBinarySearch binarySearch = new IterativeBinarySearch(); + assertEquals(-1, binarySearch.find(null, 1), "A null array should return -1."); + } }