-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp10_3.py
67 lines (50 loc) · 1.56 KB
/
p10_3.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def search_rot_arr(arr_rotated, num):
n = len(arr_rotated)
if not arr_rotated:
return -1
elif n == 1:
return 0 if arr_rotated[0] == num else -1
elif arr_rotated[0] < arr_rotated[-1]:
return binary_search(arr_rotated, 0, n-1, num)
pivot_index = find_pivot_index(arr_rotated)
res_index = -1
print(f"Found pivot index: {pivot_index}")
search_start, search_end = \
(pivot_index+1, n-1) if num <= arr_rotated[-1] else (0, pivot_index)
return binary_search(arr_rotated, search_start, search_end, num)
def binary_search(arr, s, e, target):
low = s
high = e
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
high = mid-1
else:
low = mid+1
return -1
def find_pivot_index(arr):
"""Addresable index of the last element before pivot"""
low = 0
high = len(arr)-1
while low <= high:
mid = (low+high) // 2 # Divide and floor
if mid == len(arr):
return 0
if arr[mid+1] < arr[mid]:
return mid
if arr[mid] > arr[-1]:
low = mid+1
else:
high = mid - 1
return 0
if __name__ == "__main__":
exs = [([2], 2),
([1, 2], 2),
([0, 1, 2, 3, 4, 5], 1),
([9, 12, 17, 2, 4, 5], 17),
([9, 12, 17, 2, 4, 5, 6], 4)]
for arr, target in exs:
print(
f"Looking for {target} in {arr}, found location ? {search_rot_arr(arr, target)}")