-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
LowerBound.java
35 lines (32 loc) · 1.23 KB
/
LowerBound.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.jwetherell.algorithms.search;
/**
* Lower bound search algorithm.<br>
* Lower bound is kind of binary search algorithm but:<br>
* -If searched element doesn't exist function returns index of first element which is bigger than searched value.<br>
* -If searched element is bigger than any array element function returns first index after last element.<br>
* -If searched element is lower than any array element function returns index of first element.<br>
* -If there are many values equals searched value function returns first occurrence.<br>
* Behaviour for unsorted arrays is unspecified.
* <p>
* Complexity O(log n).
* <p>
* @author Bartlomiej Drozd <[email protected]>
* @author Justin Wetherell <[email protected]>
*/
public class LowerBound {
private LowerBound() { }
public static int lowerBound(int[] array, int length, int value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
//checks if the value is less than middle element of the array
if (value <= array[mid]) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
}