- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathUpperBound.java
33 lines (30 loc) · 961 Bytes
/
UpperBound.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
packagecom.jwetherell.algorithms.search;
/**
* Upper bound search algorithm.<br>
* Upper bound is kind of binary search algorithm but:<br>
* -It returns index of first element which is grater than searched value.<br>
* -If searched element is bigger than any array element function returns first index after last element.<br>
* <br>
* Behaviour for unsorted arrays is unspecified.
* <p>
* Complexity O(log n).
* <br>
* @author Bartlomiej Drozd <mail@bartlomiejdrozd.pl>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
publicclassUpperBound {
privateUpperBound() { }
publicstaticintupperBound(int[] array, intlength, intvalue) {
intlow = 0;
inthigh = length;
while (low < high) {
finalintmid = (low + high) / 2;
if (value >= array[mid]) {
low = mid + 1;
} else {
high = mid;
}
}
returnlow;
}
}