mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2024-11-15 17:30:52 +00:00
add binary search
This commit is contained in:
parent
3fff2dcb36
commit
9d1926ad89
46
chapter1/binary.py
Normal file
46
chapter1/binary.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import logging
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
LOWER = 1000
|
||||||
|
UPPER = 1000000
|
||||||
|
SAMPLE_SIZE = 1000
|
||||||
|
|
||||||
|
|
||||||
|
def binary_search(arr, item):
|
||||||
|
count = 1
|
||||||
|
low = 0
|
||||||
|
high = len(arr) - 1
|
||||||
|
|
||||||
|
while low <= high:
|
||||||
|
mid = (low + high) // 2
|
||||||
|
guess = arr[mid]
|
||||||
|
if guess == item:
|
||||||
|
logger.debug(f"found item after {count} splits")
|
||||||
|
return mid
|
||||||
|
elif guess > item:
|
||||||
|
high = mid - 1
|
||||||
|
else:
|
||||||
|
low = mid + 1
|
||||||
|
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
|
||||||
|
numbers.sort()
|
||||||
|
logger.debug(numbers)
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
res = None
|
||||||
|
while res is None:
|
||||||
|
guess = random.randrange(LOWER, UPPER)
|
||||||
|
res = binary_search(numbers, guess)
|
||||||
|
|
||||||
|
|
||||||
|
print(f"Found {guess} at index {res}. Running time {time.time() - start}")
|
Loading…
Reference in New Issue
Block a user