Skip to content

Commit

Permalink
Update logsearch.py: binary_search correct way
Browse files Browse the repository at this point in the history
  • Loading branch information
daedalus authored Feb 10, 2024
1 parent eec6c22 commit cd0272a
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions logsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,22 @@ def search(text, pos):
count = 0

# logarithm search
def seeklog(A, key, imin, imax):
def binary_search(A, key, low, high):
global count
count += 1

if len(A) > 0:
imid = (imax + imin) / 2
if A[imid] > key:
return seeklog(A, key, imin, imid - 1)
elif A[imid] < key:
return seeklog(A, key, imid + 1, imax)
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 imid
return mid


def test():
tmp = list(range(1024 * 1024 * 20))
print("res=", seeklog(tmp, 1337, 0, len(tmp)))
print("res=", binary_search(tmp, 1337, 0, len(tmp)))
print("count=", count)


Expand Down

0 comments on commit cd0272a

Please sign in to comment.