-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearch.py
79 lines (61 loc) · 1.52 KB
/
binarySearch.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
68
69
70
71
72
73
74
75
76
77
78
79
b = [2, 3, 4, 10, 40]
cards = [
"2s","3s","4s","5s","6s","7s","8s","9s","10s","Js","Qs","Ks","As"
"2h","3h","4h","5h","6h","7h","8h","9h","10h","Jh","Qh","Kh","Ah"
"2d","3d","4d","5d","6d","7d","8d","9d","10d","Jd","Qd","Kd","Ad"
"2c","3c","4c","5c","6c","7c","8c","9c","10c","Jc","Qc","Kc"
]
def binarySearch(a, v, s=0, e=0):
# assume a is sorted ascending
e = len(a) - 1 if not e else e
#if e - s == 1:
#return s if a[s] == v else e if a[e] == v else -1
mid = s + int((e - s)/2)
if a[mid] == v:
return mid
elif a[mid] > v:
return binarySearch(a, v, s=s, e=mid)
elif a[mid] < v:
return binarySearch(a, v, s=mid, e=e)
cards.sort()
print(cards)
x = binarySearch(cards, '5h')
print(x)
x = binarySearch(b, 3)
def jumpSearch(a, v):
#m = jumpBlock = len(a)//2
# devise a formula for optimal value of jump block to achieve least traversal
res = -1
l = len(a)
m = int(l**0.5)
print('length , chunk', l, m)
for i in range(0, l, m):
#print('i', i)
e = i + m if i + m < l else l - 1
if v <= a[e]:
res = i
break
if res >= 0:
e = res + m if res + m < l else l
for res in range(res, e):
if a[res] == v:
break
return res
x = jumpSearch(cards, 'Qh')
print(x)
def exponentialSearch(a, v):
exp = 1
s = exp
res = -1
l = len(a)
while a[exp] < v:
s = exp
exp *= 2
if exp >= l:
if v < a[l - 1]:
exp = l - 1
break
if exp < l:
res = binarySearch(a, v, s, exp)
return res
print(exponentialSearch(cards, 'Qh'))