-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
58 lines (50 loc) · 1.84 KB
/
BinarySearch.java
File metadata and controls
58 lines (50 loc) · 1.84 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package algorithmsAndDataStructures.linear;
public class BinarySearch {
public static void main(String[] args) {
BinarySearch bs = new BinarySearch();
int[] nums = new int[]{1,2,2,2,4};
System.out.println(bs.higher(nums, 2)); // return 3
System.out.println(bs.higher(nums, 3)); // return 4
System.out.println(bs.higher(nums, 5)); // return 5
nums = new int[]{9,10,100,101,1002,10203};
System.out.println(bs.higher(nums, 10203));
}
// lower bound / the floor is the greatest element smaller than or equal to x.
// target exists, the first element equals to the target
// target not exists, the last largest element smaller than the target
public int lower(int[] nums, int target) {
int n = nums.length;
int start = 0, end = n - 1;
// 1 2 2 3
// 0 1 2 3
// i j
while (start < end) {
int mid = start + (end - start) / 2;
if (nums[mid] < target) {
start = mid + 1;
} else {
end = mid;
}
}
return end;
}
//upper bound / ceiling of x is the smallest element in array greater than or equal to x
// target exists, the last element equals to the target
// target not exists, the first smallest element bigger than the target
public int higher(int[] nums, int target) {
int n = nums.length;
int start = 0, end = n - 1;
// 1 2 2 4
// 0 1 2 3
// i j
while (start < end) {
int mid = start + (end - start) / 2 + 1; // think when start + 1 == end && nums[start] == target
if (nums[mid] > target) {
end = mid - 1;
} else {
start = mid;
}
}
return nums[start] < target ? start + 1 : start;
}
}