mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2026-04-16 11:33:34 +00:00
clean up repo.
add more notes
This commit is contained in:
5
chapter1/README.md
Normal file
5
chapter1/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Binary Search
|
||||
|
||||
Repeatedly split the array checking if value is greater or less than the mid point. Stop when the exact value is found.
|
||||
|
||||
It takes log N steps to reduce an array of size N to an array of size 1. Time complexity for this algorithm is `O(log N)`.
|
||||
@@ -30,11 +30,14 @@ SAMPLE_SIZE = 1000
|
||||
numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
|
||||
numbers.sort()
|
||||
|
||||
seen = set()
|
||||
count = 0
|
||||
result = None
|
||||
while result is None:
|
||||
while not result:
|
||||
guess = random.randrange(LOWER, UPPER)
|
||||
logger.debug(f"guess: {guess}")
|
||||
result = binary_search(numbers, guess)
|
||||
if guess not in seen:
|
||||
count += 1
|
||||
seen.add(guess)
|
||||
result = binary_search(numbers, guess)
|
||||
|
||||
|
||||
print(f"Found {guess} at index {result}.")
|
||||
print(f"Found {guess} at index {result} after {count} attempts")
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
import math
|
||||
|
||||
num_steps = int(math.log2(128))
|
||||
print(
|
||||
f"A binary search would take maximum {num_steps} steps "
|
||||
"to search a list of 128 items."
|
||||
)
|
||||
@@ -1,7 +0,0 @@
|
||||
import math
|
||||
|
||||
num_steps = int(math.log2(128*2))
|
||||
print(
|
||||
f"A binary search would take maximum {num_steps} steps "
|
||||
"to search a list of 256 items."
|
||||
)
|
||||
Reference in New Issue
Block a user