-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbinary-search.py
More file actions
47 lines (36 loc) · 929 Bytes
/
Copy pathbinary-search.py
File metadata and controls
47 lines (36 loc) · 929 Bytes
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
from random import randint
x = sorted([ randint(0, 100) for i in xrange(0, 100) ])
search = 32
print("x: {}".format(x))
print("search: {}".format(search))
def binary_search(x, search):
found = -1
p = 0
r = len(x) - 1
while found == -1 and r >= p:
q = (p + r) / 2
if x[q] == search:
found = q
if x[q] < search:
p = q + 1
elif x[q] > search:
r = q - 1
return found
def recursive_binary_search(x, search, p=0, r=(len(x) - 1)):
q = (p + r) / 2
if p > r:
return -1
if x[q] == search:
return q
elif x[q] < search:
return recursive_binary_search(
x, search,
(q + 1), r,
)
elif x[q] > search:
return recursive_binary_search(
x, search,
p, (q - 1),
)
print binary_search(x, search)
print recursive_binary_search(x, search)