Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
int search(int A[], int n, int target){ int start = 0, end = n, middle ; while(start < end){ middle = (start + end) / 2; if(A[middle] == target) return middle; if(A[middle] > A[start]){ if(target >= A[start] && target < A[middle]){ end = middle; }else{ start = middle + 1; } }else if(A[middle] < A[start]){ if(target > A[middle] && target <= A[end - 1]){ start = middle + 1; }else{ end = middle; } } else{ ++start; } } return -1; }