-
Notifications
You must be signed in to change notification settings - Fork 43
/
binary_search.py
62 lines (57 loc) · 1.8 KB
/
binary_search.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
#---------------------------------------------------------------
# BINARY SEARCH
#---------------------------------------------------------------
# V0
# V1
# http://kuanghy.github.io/2016/06/14/python-bisect
# Binary search via recursion
def binary_search_recursion(lst, value, low, high):
if high < low:
return None
# better to use mid = low + (high - low) / 2 then mid = (low + high) / 2
# since "low + high" may cause "stack overflow" issue when low, high are some big number
mid = low + (high - low) / 2
if lst[mid] > value:
return binary_search_recursion(lst, value, low, mid-1)
elif lst[mid] < value:
return binary_search_recursion(lst, value, mid+1, high)
else:
return mid
# Binary search via for loop
def binary_search_loop(lst,value):
low, high = 0, len(lst)-1
while low <= high:
mid = (low + high) / 2
if lst[mid] < value:
low = mid + 1
elif lst[mid] > value:
high = mid - 1
else:
return mid
return None
# V2
# https://github.com/yennanliu/algorithms/blob/master/algorithms/search/binary_search.py
# Binary search via for loop
def binary_search(array, query):
lo, hi = 0, len(array) - 1
while lo <= hi:
mid = (hi + lo) // 2
val = array[mid]
if val == query:
return mid
elif val < query:
lo = mid + 1
else:
hi = mid - 1
return None
# Binary search via recursion
def binary_search_recur(array, low, high, val):
if low > high: # error case
return -1
mid = (low + high) // 2
if val < array[mid]:
return binary_search_recur(array, low, mid - 1, val)
elif val > array[mid]:
return binary_search_recur(array, mid + 1, high, val)
else:
return mid