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,10 +26,13 @@ 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 ) {
33+ if (array == null || array .length == 0 || key == null ) {
34+ return 0 ;
35+ }
3336 return search (array , key , 0 , array .length - 1 );
3437 }
3538
@@ -51,10 +54,8 @@ private <T extends Comparable<T>> int search(T[] array, T key, int left, int rig
5154 int median = (left + right ) >>> 1 ;
5255 int comp = key .compareTo (array [median ]);
5356
54- if (comp == 0 ) {
55- return median ;
56- } else if (comp < 0 ) {
57- // median position can be a possible solution
57+ if (comp <= 0 ) {
58+ // key is less than or equal to array[median], median position can be a possible solution
5859 return search (array , key , left , median );
5960 } else {
6061 // key we are looking is greater, so we must look on the right of median position
0 commit comments