mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create bs_rotated_array.py
This commit is contained in:
parent
2626cec18a
commit
a449abf709
46
searching/bs_rotated_array.py
Normal file
46
searching/bs_rotated_array.py
Normal file
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
def search(nums):
|
||||
|
||||
# 1. find the smallest element in the rotate array
|
||||
# "left" will be used as index later
|
||||
|
||||
left, right = 0, len(nums)
|
||||
while left < right:
|
||||
|
||||
mid = (left + right) // 2
|
||||
if nums[mid] > nums[-1]:
|
||||
left = mid + 1
|
||||
else:
|
||||
right = mid
|
||||
|
||||
|
||||
# 2. write a binary search
|
||||
def bs(left, right, target):
|
||||
|
||||
while left < right:
|
||||
|
||||
mid = (left + right) // 2
|
||||
|
||||
if nums[mid] == target:
|
||||
return mid
|
||||
|
||||
elif nums[mid] > target:
|
||||
right = mid
|
||||
|
||||
else:
|
||||
left = mid + 1
|
||||
|
||||
return -1
|
||||
|
||||
# 3. run for both sides
|
||||
response = bs(0, left, target)
|
||||
if response != -1:
|
||||
return response
|
||||
|
||||
else:
|
||||
return bs(left, len(nums), target)
|
||||
|
Loading…
x
Reference in New Issue
Block a user