Skip to content

Commit 4d50b7f

Browse files
committed
docs(searches): document null return behavior, update best-case complexity, and test UpperBound duplicates
1 parent 2f20fa8 commit 4d50b7f

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* This is an extension of BinarySearch.
1414
*
1515
* <p>
16-
* Worst-case performance O(log n) Best-case performance O(1) Average
16+
* Worst-case performance O(log n) Best-case performance O(log n) Average
1717
* performance O(log n) Worst-case space complexity O(1)
1818
*
1919
* @author Pratik Padalia (https://github.com/15pratik)
@@ -26,7 +26,7 @@ class LowerBound implements SearchAlgorithm {
2626
* @param array is an array where the LowerBound value is to be found
2727
* @param key is an element for which the LowerBound is to be found
2828
* @param <T> is any comparable type
29-
* @return index of the LowerBound element
29+
* @return index of the first element not less than key, or 0 if array is null/empty or key is null
3030
*/
3131
@Override
3232
public <T extends Comparable<T>> int find(T[] array, T key) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* This is an extension of BinarySearch.
1414
*
1515
* <p>
16-
* Worst-case performance O(log n) Best-case performance O(1) Average
16+
* Worst-case performance O(log n) Best-case performance O(log n) Average
1717
* performance O(log n) Worst-case space complexity O(1)
1818
*
1919
* @author Pratik Padalia (https://github.com/15pratik)
@@ -26,7 +26,7 @@ class UpperBound implements SearchAlgorithm {
2626
* @param array is an array where the UpperBound value is to be found
2727
* @param key is an element for which the UpperBound is to be found
2828
* @param <T> is any comparable type
29-
* @return index of the UpperBound element
29+
* @return index of the first element greater than key, last index if no such element exists, or 0 if array is null/empty or key is null
3030
*/
3131
@Override
3232
public <T extends Comparable<T>> int find(T[] array, T key) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ void testUpperBoundMultipleValues() {
5757
assertTrue(arrayWithDuplicates[index] > key, "Element at the upper bound index should be greater than the key.");
5858
}
5959

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

0 commit comments

Comments
 (0)