-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInRotatedArray.java
More file actions
54 lines (51 loc) · 1.46 KB
/
SearchInRotatedArray.java
File metadata and controls
54 lines (51 loc) · 1.46 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
package algorithm.search;
/**
* The type Search in rotated array.
*/
public class SearchInRotatedArray {
/**
* Search boolean.
*
* @param array the array
* @param a the a
* @param left the left
* @param right the right
* @return the boolean
*/
public static boolean search(int[] array, int a ,int left, int right){
int mid = (left+right)/2;
if(a==array[mid])
return true;
if (right < left)
return false;
if(array[left]<array[mid]){
if(a>=array[left] && a<array[mid]){
return search(array,a,left,mid-1);
}else{
return search(array,a,mid+1,right);
}
}else if(array[mid]<array[left]){
if(a>=array[mid] && a<array[right]){
return search(array,a,mid+1,right);
}else{
return search(array,a,left,mid-1);
}
}else{// Otherwise, search both halves
boolean result = search(array,a, left, mid - 1); // Search in left
if (!result)
return search(array, a,mid + 1, right); // Search in right
else
return result;
}
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[])
{
int arr[] = {40,40,40,100,-100,40,0,24,40};
System.out.println( "The index is: " + search(arr, -100,0, 8));
}
}