Leetcode: Search in rotated array

Rachit Gupta

511

We can even directly search, without finding the pivot element first and then searching for the element:

# assumption: element has to be in array.

find(array, left, right):

mid = (left + right)/2

if array[mid] == element:

return mid

else:

if array[mid] <= array[right]:

if element > array[mid] and element <= array[right]:

find(array, mid + 1, right)

else:

find(array, left, mid -1)

else:

if element >= array[left] and element < array[mid]:

find(array, left, mid-1)

else:

find(array, mid + 1, right)