-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinterviewbit-binary-search-rotated-sorted-array-search.java
More file actions
45 lines (44 loc) · 1.21 KB
/
interviewbit-binary-search-rotated-sorted-array-search.java
File metadata and controls
45 lines (44 loc) · 1.21 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
public class Solution {
// DO NOT MODIFY THE LIST
public int binSearch(final List<Integer> a, int b, int low, int high) {
int med;
while (low <= high) {
med = (low + high) / 2;
if (a.get(med) == b) {
return med;
} else if (a.get(med) < b) {
low = med + 1;
} else {
high = med - 1;
}
}
return -1;
}
public int search(final List<Integer> a, int b) {
int low, high, med, pivot;
if (a.get(0) < a.get(a.size() - 1)) {
// no pivot
return binSearch(a, b, 0, a.size() - 1);
}
// find pivot
low = 1;
high = a.size() - 1;
while (low <= high) {
med = (low + high) / 2;
if (a.get(med) < a.get(0)) {
high = med - 1;
} else {
low = med + 1;
}
}
pivot = low;
if (b < a.get(pivot) || b > a.get(pivot - 1)) {
return -1;
}
if (b >= a.get(0)) {
return binSearch(a, b, 0, pivot - 1);
} else {
return binSearch(a, b, pivot, a.size() - 1);
}
}
}