-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchRotated.java
More file actions
35 lines (30 loc) · 890 Bytes
/
SearchRotated.java
File metadata and controls
35 lines (30 loc) · 890 Bytes
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 Searching;
public class SearchRotated {
static int search(int[] a, int target) {
int n = a.length - 1;
int st = 0, end = n - 1;
while (st <= end) {
int mid = st + (end - st) / 2;
if (a[mid] == target) {
return mid;
} else if (a[mid] <= a[end]) {
if (target > a[mid] && target <= a[end]) {
st = mid + 1;
} else {
end = mid - 1;
}
} else {
if (target >= a[st] && target < a[mid]) {
end = mid - 1;
} else {
st = mid + 1;
}
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {4, 5, 6, 7, 1, 2, 3};
System.out.println(search(arr, 4));
}
}