mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2026-04-16 11:33:34 +00:00
reorganise directories
upd chapter_01/binary.py
This commit is contained in:
9
chapter_04/README.md
Normal file
9
chapter_04/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Quicksort
|
||||
|
||||
Similar to the previous recursive function, quicksort uses divide and conquer.
|
||||
|
||||
The base case occurs for an array size 0 or 1 (doesn't need to be sorted).
|
||||
|
||||
The recursive case works by partitioning the array around a chosen pivot repeatedly until the base case is met and then combining all sorted sub-arrays.
|
||||
|
||||
Note. Quicksort should be implemented using a random pivot to ensure average runtimes.
|
||||
13
chapter_04/ex4.1.py
Normal file
13
chapter_04/ex4.1.py
Normal file
@@ -0,0 +1,13 @@
|
||||
class Number:
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
|
||||
def summation(nums):
|
||||
if len(nums) == 1:
|
||||
return nums[0].value
|
||||
return nums[0].value + summation(nums[1:])
|
||||
|
||||
|
||||
nums = [Number(i) for i in range(0, 100, 3)]
|
||||
print(summation(nums))
|
||||
16
chapter_04/ex4.3.py
Normal file
16
chapter_04/ex4.3.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import logging
|
||||
import random
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def maximum(nums, size):
|
||||
if size == 1:
|
||||
return nums[0]
|
||||
return max(nums[size - 1], maximum(nums, size - 1))
|
||||
|
||||
|
||||
randomlist = random.sample(range(10, 300), 5)
|
||||
logger.debug(randomlist)
|
||||
print(maximum(randomlist, len(randomlist)))
|
||||
45
chapter_04/ex4.4.py
Normal file
45
chapter_04/ex4.4.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import logging
|
||||
import random
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def binary_search(arr, low, high, x):
|
||||
# x not found, low has overlapped high so we return None
|
||||
if high < low:
|
||||
return
|
||||
|
||||
mid = (high + low) // 2
|
||||
|
||||
# x found at middle of split, return its index
|
||||
if arr[mid] == x:
|
||||
return mid
|
||||
|
||||
# x in left side of split
|
||||
elif arr[mid] > x:
|
||||
return binary_search(arr, low, mid - 1, x)
|
||||
|
||||
# x in right side of split
|
||||
else:
|
||||
return binary_search(arr, mid + 1, high, x)
|
||||
|
||||
|
||||
LOWER = 1000
|
||||
UPPER = 1000000
|
||||
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)
|
||||
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} after {count} attempts.")
|
||||
27
chapter_04/quicksort.py
Normal file
27
chapter_04/quicksort.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import logging
|
||||
import random
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def quicksort(arr):
|
||||
# base case. arr of length 0 or 1 don't need sorting, so return them as is
|
||||
if len(arr) < 2:
|
||||
return arr
|
||||
|
||||
pivot = arr[0]
|
||||
|
||||
# split arr into 3 parts, [less] | [pivot] | [greater]
|
||||
less = [i for i in arr[1:] if i <= pivot]
|
||||
greater = [i for i in arr[1:] if i > pivot]
|
||||
|
||||
return quicksort(less) + [pivot] + quicksort(greater)
|
||||
|
||||
|
||||
LOWER = 0
|
||||
UPPER = 100
|
||||
SAMPLE_SIZE = 15
|
||||
numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
|
||||
logging.debug(numbers)
|
||||
print(quicksort(numbers))
|
||||
Reference in New Issue
Block a user