-
Notifications
You must be signed in to change notification settings - Fork 11
/
logsearch.py
51 lines (38 loc) · 1.01 KB
/
logsearch.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
#!/usr/bin/env python
#
# Author Dario Clavijo 2015
# GPLv3
# linear search
def seek(A, key, imin, imax):
for _ in range(imin, imax):
if A[n] == key:
return v
def search(text, pos):
current_char = text[pos]
accum = ""
while current_char.isdigit() and pos <= len(text) - 1:
current_char = text[pos]
if current_char.isdigit():
accum = accum + current_char
pos += 1
if accum.isdigit():
return int(accum)
print(search("123456789--0", 0))
count = 0
# logarithm search
def binary_search(A, key, low, high):
global count
count += 1
if len(A) > 0:
mid = low + ((high - low) >> 1);
if A[mid] > key:
return binary_search(A, key, low, mid - 1)
elif A[mid] < key:
return binary_search(A, key, mid + 1, high)
else:
return mid
def test():
tmp = list(range(1024 * 1024 * 20))
print("res=", binary_search(tmp, 1337, 0, len(tmp)))
print("count=", count)
test()