-
Notifications
You must be signed in to change notification settings - Fork 2
/
binsearch.py
46 lines (42 loc) · 1.2 KB
/
binsearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def search_in_rotated_sorted_array(A, value):
"""
>>> search_in_rotated_sorted_array([4, 5, 6, 7, 0, 1, 2], 6)
2
>>> search_in_rotated_sorted_array([4, 5, 6, 7, 0, 1, 2], 0)
4
>>> search_in_rotated_sorted_array([5, 6, 7, 0, 1, 2, 4], 6)
1
"""
def find_pivot(A):
left = 0
right = len(A) - 1
while left < right:
if A[left] < A[right]:
return left
middle = left + (right - left) / 2
if A[left] < A[middle]:
left = middle + 1
else:
right = middle
return left
def binary_search(A, left, right, value):
while left < right:
middle = left + (right - left) / 2
if value == A[middle]:
return middle
elif value < A[middle]:
right = middle - 1
else:
left = middle + 1
if A[left] == value:
return left
else:
return -1
pivot = find_pivot(A)
index = binary_search(A, 0, pivot-1, value)
if index >= 0:
return index
else:
return binary_search(A, pivot, len(A)-1, value)
import doctest
doctest.testmod()