Three cases:
0 1 2 3 4 5 6 7
2 3 4 5 6 7 0 1
6 7 0 1 2 3 4 5
int search(int A[], int target) {
int left = 0, right = A.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (A[mid] == target) {
return mid;
}
if (A[mid] >= A[left]) {
if (A[left] <= target && target < A[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if (A[mid] < target && target <= A[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
refer: http://fisherlei.blogspot.com/2013/01/leetcode-search-in-rotated-sorted-array.html