Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/main/java/com/thealgorithms/searches/BinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class BinarySearch implements SearchAlgorithm {
* // notFound will be -1 (element 4 does not exist)
* </pre>
*
* @param <T> The type of elements in the array (must be Comparable)
* @param <T> 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
Expand Down Expand Up @@ -89,10 +89,10 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
* <p>Time Complexity: O(log n) because we halve the search space each time.
* Space Complexity: O(log n) due to recursive call stack.
*
* @param <T> The type of elements (must be Comparable)
* @param <T> 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
*/
Expand Down Expand Up @@ -132,3 +132,5 @@ else if (comp < 0) {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ 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 <T> type of elements (must be Comparable)
* @param key the element to search
* @param <T> type of elements (must be Comparable)
* @return index of the key if found, otherwise -1
*/
@Override
public <T extends Comparable<T>> 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;
}

int left = 0;
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]);

Expand All @@ -53,6 +57,7 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
}
}

// Element not found
return -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
Loading