Skip to content

Instantly share code, notes, and snippets.

@conr
Created January 21, 2018 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conr/78751c9e465c19335c0a182c718ecd25 to your computer and use it in GitHub Desktop.
Save conr/78751c9e465c19335c0a182c718ecd25 to your computer and use it in GitHub Desktop.
Search in rotated sorted array
def search(nums, target)
if nums.length == 0
return -1
end
low, high = 0, nums.length-1
while low <= high
mid = (high+low)/2
if nums[mid] == target
return mid
end
if nums[low] <= nums[mid]
if nums[low] <= target && target < nums[mid]
high = mid - 1
else
low = mid + 1
end
else
if nums[mid] < target && target <= nums[high]
low = mid + 1
else
high = mid - 1
end
end
end
return -1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment