-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotatedSort.java
37 lines (34 loc) · 1.02 KB
/
RotatedSort.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
public class RotatedSort {
public static int search(int arr[], int target, int start, int end){
if(start>end){
return -1; //base
}
int mid= (start+end)/2;
if(arr[mid]==target){
return mid;
}
if(arr[start]<=arr[mid]){
if(arr[start]<=target && target<=arr[mid]){
//target is on line 1
return search(arr, target, start, mid-1);
}else{
return search(arr, target, mid+1,end);
}
}
else{
if(arr[mid]<=target && target<=arr[end]){
//target is on line 2
search(arr, target, mid+1, end);
}else{
return search(arr, target, start, mid-1);
}
}
return 1;
}
public static void main (String args[]){
int arr[]={4,5,6,7,0,1,2};
int target =6;
int tar = search(arr, target, 0, arr.length-1);
System.out.println("Index = "+tar);
}
}