left
to 0 and right
to n - 1
.left <= right
:
a. Calculate mid
as the midpoint between left
and right
.
b. If nums[mid]
is equal to target
, return mid
.
c. If nums[mid]
is greater than or equal to nums[left]
, the left half is sorted.
i. If target
is between nums[left]
and nums[mid]
, set right
to mid - 1
.
ii. Else, set left
to mid + 1
.
d. Else, the right half is sorted.
i. If target
is between nums[mid]
and nums[right]
, set left
to mid + 1
.
ii. Else, set right
to mid - 1
.