Skip to content

Commit 81c661e

Browse files
committed
fix(searches): require non-null arguments, update best-case complexity, and test UpperBound duplicates
1 parent 2f20fa8 commit 81c661e

4 files changed

Lines changed: 29 additions & 10 deletions

File tree

‎src/main/java/com/thealgorithms/searches/LowerBound.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.searches;
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4+
import java.util.Objects;
45

56
/**
67
* The LowerBound method is used to return an index pointing to the first
@@ -13,7 +14,7 @@
1314
* This is an extension of BinarySearch.
1415
*
1516
* <p>
16-
* Worst-case performance O(log n) Best-case performance O(1) Average
17+
* Worst-case performance O(log n) Best-case performance O(log n) Average
1718
* performance O(log n) Worst-case space complexity O(1)
1819
*
1920
* @author Pratik Padalia (https://github.com/15pratik)
@@ -26,11 +27,14 @@ class LowerBound implements SearchAlgorithm {
2627
* @param array is an array where the LowerBound value is to be found
2728
* @param key is an element for which the LowerBound is to be found
2829
* @param <T> is any comparable type
29-
* @return index of the LowerBound element
30+
* @return index of the first element not less than key, or 0 if array is empty
31+
* @throws NullPointerException if {@code array} or {@code key} is null
3032
*/
3133
@Override
3234
public <T extends Comparable<T>> int find(T[] array, T key) {
33-
if (array == null || array.length == 0 || key == null) {
35+
Objects.requireNonNull(array, "array must not be null");
36+
Objects.requireNonNull(key, "key must not be null");
37+
if (array.length == 0) {
3438
return 0;
3539
}
3640
return search(array, key, 0, array.length - 1);

‎src/main/java/com/thealgorithms/searches/UpperBound.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.searches;
22

33
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4+
import java.util.Objects;
45

56
/**
67
* The UpperBound method is used to return an index pointing to the first
@@ -13,7 +14,7 @@
1314
* This is an extension of BinarySearch.
1415
*
1516
* <p>
16-
* Worst-case performance O(log n) Best-case performance O(1) Average
17+
* Worst-case performance O(log n) Best-case performance O(log n) Average
1718
* performance O(log n) Worst-case space complexity O(1)
1819
*
1920
* @author Pratik Padalia (https://github.com/15pratik)
@@ -26,11 +27,14 @@ class UpperBound implements SearchAlgorithm {
2627
* @param array is an array where the UpperBound value is to be found
2728
* @param key is an element for which the UpperBound is to be found
2829
* @param <T> is any comparable type
29-
* @return index of the UpperBound element
30+
* @return index of the first element greater than key, last index if no such element exists, or 0 if array is empty
31+
* @throws NullPointerException if {@code array} or {@code key} is null
3032
*/
3133
@Override
3234
public <T extends Comparable<T>> int find(T[] array, T key) {
33-
if (array == null || array.length == 0 || key == null) {
35+
Objects.requireNonNull(array, "array must not be null");
36+
Objects.requireNonNull(key, "key must not be null");
37+
if (array.length == 0) {
3438
return 0;
3539
}
3640
return search(array, key, 0, array.length - 1);

‎src/test/java/com/thealgorithms/searches/LowerBoundTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.searches;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
67

@@ -80,7 +81,7 @@ void testLowerBoundNullAndEmptyEdgeCases() {
8081
LowerBound lowerBound = new LowerBound();
8182
Integer[] emptyArray = {};
8283
assertEquals(0, lowerBound.find(emptyArray, 5), "Lower bound for an empty array should be 0");
83-
assertEquals(0, lowerBound.find(null, 5), "Lower bound for a null array should be 0");
84-
assertEquals(0, lowerBound.find(new Integer[] {1, 2, 3}, null), "Lower bound for a null key should be 0");
84+
assertThrows(NullPointerException.class, () -> lowerBound.find(null, 5));
85+
assertThrows(NullPointerException.class, () -> lowerBound.find(new Integer[] {1, 2, 3}, null));
8586
}
8687
}

‎src/test/java/com/thealgorithms/searches/UpperBoundTest.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.searches;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import java.util.Random;
@@ -57,6 +58,15 @@ void testUpperBoundMultipleValues() {
5758
assertTrue(arrayWithDuplicates[index] > key, "Element at the upper bound index should be greater than the key.");
5859
}
5960

61+
@Test
62+
void testUpperBoundDuplicates() {
63+
Integer[] arrayWithDuplicates = {1, 2, 2, 2, 3};
64+
assertEquals(4, upperBound.find(arrayWithDuplicates, 2), "Upper bound for 2 in {1, 2, 2, 2, 3} should be at index 4");
65+
66+
Integer[] allSame = {2, 2, 2, 2, 2};
67+
assertEquals(4, upperBound.find(allSame, 2), "Upper bound for 2 in all-same array should be at last index 4");
68+
}
69+
6070
@Test
6171
void testUpperBoundLowerThanMin() {
6272
int key = 0; // Test with a key lower than the minimum element
@@ -99,7 +109,7 @@ void testUpperBoundSingleElementArray() {
99109

100110
@Test
101111
void testUpperBoundNullArrayAndKey() {
102-
assertEquals(0, upperBound.find(null, 5), "Upper bound for a null array should be 0.");
103-
assertEquals(0, upperBound.find(new Integer[] {1, 2, 3}, null), "Upper bound for a null key should be 0.");
112+
assertThrows(NullPointerException.class, () -> upperBound.find(null, 5));
113+
assertThrows(NullPointerException.class, () -> upperBound.find(new Integer[] {1, 2, 3}, null));
104114
}
105115
}

0 commit comments

Comments
 (0)