clean up repo.

add more notes
This commit is contained in:
2024-02-06 21:53:17 +00:00
parent d552050f7e
commit 3fe14b8ac0
13 changed files with 52 additions and 60 deletions

5
chapter1/README.md Normal file
View 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)`.

View File

@@ -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")

View File

@@ -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."
)

View File

@@ -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."
)