-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathBinarySearch.java
61 lines (49 loc) · 1.7 KB
/
BinarySearch.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package algo.sortingandsearching;
/**
* Created by sherxon on 1/23/17.
*/
public class BinarySearch {
/**
* This is binary search algorithm, Time complexity is O(lgN) and does use extra space.
* This finds any occurrence of given key
*/
int search(int[] a, int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] == key) return mid; // key found
if (a[mid] < key) lo = mid + 1;
else hi = mid - 1;
}
return -(lo + 1); // key not found, position to be inserted is returned
}
/**
* When there are duplicates, this finds the first occurrence of the key
*/
int searchLowerBound(int[] a, int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] == key && (mid == 0 || a[mid - 1] != key)) return mid; // key found
if (a[mid] < key) lo = mid + 1; // pay attention
else hi = mid - 1;
}
return -(lo + 1); // key not found, position to be inserted is returned
}
/**
* When there are duplicates, this finds the last occurrence of the key
*/
int searchUpperBound(int[] a, int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] == key && (mid == a.length - 1 || a[mid + 1] != key)) return mid; // key found
if (a[mid] <= key) lo = mid + 1; // pay attention
else hi = mid - 1;
}
return -(hi + 1); // key not found, position to be inserted is returned
}
}