As for your question, your assumption is perfectly correct that you do not need to look for both halves. You can simply search for the right half, since if a[mid] != key , your element will undoubtedly be in the right half if it is in an array.
EDIT:
The above conclusion is incomplete. Now I will write a new one, with further reflection.
Firstly, if you are looking for both halves at any given time, it makes no sense to do a binary search, since the complexity of your search becomes O(n) .
Now you are right there, that if a[mid] == a[left] , your key can be in any of the halves. But, on the one hand, you can be sure that one of the halves will have all elements equal.
eg. Suppose a[12] = [1,1,1,2,2,2,2,2,2,2,2,2] rotate r=2 times, a`[12] = [2,2,1,1,1,2,2,2,2,2,2,2] so, a[mid] = a[left] = 2 if key = 1, it is in left half. now, rotate r=9 times, a``[12] = [2,2,2,2,2,2,2,2,2,1,1,1] so, a[mid] = a[left] = 2 if key = 1, it is in right half
So, you need to search both halves of this array for your key, and this case actually makes the binary search redundant.
So, now I even suggest you use the solution mentioned below.
Although, if there are no restrictions, I would like to offer him a solution:
This is a simple variation of the Binary Search algorithm.
First you need to apply a binary search in an array with a termination condition:
a[mid-1] > a[mid]
Once this condition is met, you have the index k = mid , which gives you 2 sorted arrays.
The first array, A[0...k-1] and the second array, A[k...n-1] , assuming n elements.
Now just check.
if(key > A[0]) Binary Search in A[0...k-1] else Binary Search in A[k...n-1]
One exception, if the array was rotated n times, you will not get the value k . Binary search of the entire array for a key.
EDIT2: Answering your questions from the comments
I am interested in my original method if [left] <a [mid] and x> = a [left] && x <a [mid], if it is safe to search only on the left side using search (a, left, mid-1, x); if the array can be rotated n times?
If a[left] < a[mid] , then we can be sure that a[left...mid] increases in order (sorted). So, if x >= a[left] && x < a[mid] , searching only in the left half is enough.
if you can also specify when [left] == โโa [mid] and [mid] == a [right], do we need to search in both directions?
Yes. In this case, we need to look for both directions. Say for example. your original array is {1,2,2,2,2,2,2,2,2,2} that rotates once. The array becomes, {2,1,2,2,2,2,2,2,2,2} . If he turns 8 times, he becomes {2,2,2,2,2,2,2,2,1,2} . Now, if your search key is 1 , right at the beginning, in both cases, a[mid] == a[left] == a[right] , and your key is in different halves. So you need to look for both halves for the key.