This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# A sorted array is rotated at some unknown point, Search given element in it
# Time-complexity: O(logn),Auxiliary-space:O(1)
# Algorithm: Binary-Search
defsearch(a,key)
lo=0
hi=a.length-1
whilelo<=hi
mid=lo+(hi-lo)/2
ifa[mid]==key
returnmid
elsifa[mid]>=a[lo]
if(a[mid]>key&&a[lo]<=key)
hi=mid-1
else
lo=mid+1
end
else
if(a[mid]<key&&a[hi]>=key)
lo=mid+1
else
hi=mid-1
end
end
end
return-1
end
# It doesn’t look possible to search in O(Logn) time in all cases when duplicates are allowed.