-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchAlmostSorted.java
More file actions
57 lines (52 loc) · 1.5 KB
/
SearchAlmostSorted.java
File metadata and controls
57 lines (52 loc) · 1.5 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
//http://www.geeksforgeeks.org/search-almost-sorted-array/
//The crux of the solution lies in the fact that only one of the following three can occur
// 1. previous element is greater than the current element
// 2. current element is greater than the next element
// 3. current element is greater than the previous element
// no two of the above can happen simultaneously
// if 1, then the current element is sitting where the previous element should have been if the array was sorted.
// so, the candidate array for checking for existence of val, following the logic of binary search is from the position
// of the current element to the last. Otherwise from beginning to this position
public class Solution{
public static void main(String args[]){
int[] arr = {1, 12, 22, 32, 33, 40, 45};
System.out.println(binarySearchModified(arr, 0, arr.length-1, 2));
}
public static int binarySearchModified(int[] arr, int b, int e, int val){
if(b > e) return -1;
int mid = (b + e) >> 1;
if(arr[mid] == val) return mid;
int begin, end;
if(mid > 0 && arr[mid] < arr[mid-1]){
if(arr[mid-1] < val){
begin = mid;
end = e;
}
else {
begin = b;
end = mid;
}
}
else if(mid < arr.length-1 && arr[mid] > arr[mid+1]){
if(arr[mid+1] < val){
begin = mid;
end = e;
}
else {
begin = b;
end = mid;
}
}
else{
if(arr[mid] < val) {
begin = mid+1;
end = e;
}
else{
begin = b;
end = mid-1;
}
}
return binarySearchModified(arr,begin,end,val);
}
}