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:
12
chapter4/README.md
Normal file
12
chapter4/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# Recursion
|
||||
|
||||
Recursive functions must have both:
|
||||
|
||||
- one or more base cases
|
||||
- a recursive case
|
||||
|
||||
The base cases are required to ensure the recursion stops when meeting a condition
|
||||
|
||||
The recursive case adds functions onto the call stack and completes each one top down.
|
||||
|
||||
Note. Quicksort should be implemented using a random pivot to ensure average runtimes.
|
||||
@@ -32,10 +32,14 @@ SAMPLE_SIZE = 1000
|
||||
numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
|
||||
numbers.sort()
|
||||
|
||||
seen = set()
|
||||
count = 0
|
||||
result = None
|
||||
while result is None:
|
||||
guess = random.randrange(LOWER, UPPER)
|
||||
logger.debug(f"guess: {guess}")
|
||||
result = binary_search(numbers, 0, len(numbers) - 1, guess)
|
||||
if guess not in seen:
|
||||
count += 1
|
||||
seen.add(guess)
|
||||
result = binary_search(numbers, 0, len(numbers) - 1, guess)
|
||||
|
||||
print(f"Found {guess} at index {result}.")
|
||||
print(f"Found {guess} at index {result} after {count} attempts.")
|
||||
|
||||
Reference in New Issue
Block a user