Compare commits
	
		
			2 Commits
		
	
	
		
			ab09aa5269
			...
			7e44b37850
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 7e44b37850 | |||
| 4cd7bb7d18 | 
							
								
								
									
										20
									
								
								README.md
									
									
									
									
									
								
							
							
						
						@ -40,13 +40,13 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
[chapter12][knn]
 | 
					[chapter12][knn]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[binary]: ./chapter1/
 | 
					[binary]: ./chapter_01/
 | 
				
			||||||
[selection_sort]: ./chapter2/
 | 
					[selection_sort]: ./chapter_02/
 | 
				
			||||||
[recursion]: ./chapter3/
 | 
					[recursion]: ./chapter_03/
 | 
				
			||||||
[quick_sort]: ./chapter4/
 | 
					[quick_sort]: ./chapter_04/
 | 
				
			||||||
[bfs]: ./chapter6/
 | 
					[bfs]: ./chapter_06/
 | 
				
			||||||
[trees]: ./chapter7/
 | 
					[trees]: ./chapter_07/
 | 
				
			||||||
[dijkstra]: ./chapter9/
 | 
					[dijkstra]: ./chapter_09/
 | 
				
			||||||
[greedy]: ./chapter10/
 | 
					[greedy]: ./chapter_10/
 | 
				
			||||||
[dynamic]: ./chapter11/
 | 
					[dynamic]: ./chapter_11/
 | 
				
			||||||
[knn]: ./chapter12/
 | 
					[knn]: ./chapter_12/
 | 
				
			||||||
 | 
				
			|||||||
@ -1,43 +0,0 @@
 | 
				
			|||||||
import logging
 | 
					 | 
				
			||||||
import random
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
logging.basicConfig(level=logging.DEBUG)
 | 
					 | 
				
			||||||
logger = logging.getLogger(__name__)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def binary_search(arr, item):
 | 
					 | 
				
			||||||
    low = 0
 | 
					 | 
				
			||||||
    high = len(arr) - 1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    while low <= high:
 | 
					 | 
				
			||||||
        mid = (low + high) // 2
 | 
					 | 
				
			||||||
        guess = arr[mid]
 | 
					 | 
				
			||||||
        if guess == item:
 | 
					 | 
				
			||||||
            return mid
 | 
					 | 
				
			||||||
        elif guess > item:
 | 
					 | 
				
			||||||
            high = mid - 1
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            low = mid + 1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return None
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
LOWER = 1000
 | 
					 | 
				
			||||||
UPPER = 1000000
 | 
					 | 
				
			||||||
SAMPLE_SIZE = 1000
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
 | 
					 | 
				
			||||||
numbers.sort()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
seen = set()
 | 
					 | 
				
			||||||
count = 0
 | 
					 | 
				
			||||||
result = None
 | 
					 | 
				
			||||||
while not result:
 | 
					 | 
				
			||||||
    guess = random.randrange(LOWER, UPPER)
 | 
					 | 
				
			||||||
    if guess not in seen:
 | 
					 | 
				
			||||||
        count += 1
 | 
					 | 
				
			||||||
        seen.add(guess)
 | 
					 | 
				
			||||||
        result = binary_search(numbers, guess)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
print(f"Found {guess} at index {result} after {count} attempts")
 | 
					 | 
				
			||||||
							
								
								
									
										43
									
								
								chapter_01/binary.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						@ -0,0 +1,43 @@
 | 
				
			|||||||
 | 
					import logging
 | 
				
			||||||
 | 
					import random
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					logging.basicConfig(level=logging.DEBUG)
 | 
				
			||||||
 | 
					logger = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class BinarySearch:
 | 
				
			||||||
 | 
					    def __init__(self, arr):
 | 
				
			||||||
 | 
					        self._arr = arr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def search(self, item):
 | 
				
			||||||
 | 
					        low = 0
 | 
				
			||||||
 | 
					        high = len(self._arr) - 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        while low <= high:
 | 
				
			||||||
 | 
					            mid = (low + high) // 2
 | 
				
			||||||
 | 
					            guess = self._arr[mid]
 | 
				
			||||||
 | 
					            if guess == item:
 | 
				
			||||||
 | 
					                return mid
 | 
				
			||||||
 | 
					            elif guess > item:
 | 
				
			||||||
 | 
					                high = mid - 1
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                low = mid + 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					LOWER = 1000
 | 
				
			||||||
 | 
					UPPER = 1000000
 | 
				
			||||||
 | 
					SAMPLE_SIZE = 1000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					numbers = random.sample(range(LOWER, UPPER), SAMPLE_SIZE)
 | 
				
			||||||
 | 
					numbers.extend([9000, 999999])
 | 
				
			||||||
 | 
					numbers.sort()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					search = BinarySearch(numbers)
 | 
				
			||||||
 | 
					print(search.search(42))
 | 
				
			||||||
 | 
					print(search.search(9000))
 | 
				
			||||||
 | 
					print(search.search(20000))
 | 
				
			||||||
 | 
					print(search.search(60000))
 | 
				
			||||||
 | 
					print(search.search(999999))
 | 
				
			||||||
| 
		 Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB  | 
| 
		 Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB  | 
| 
		 Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB  | 
| 
		 Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB  | 
| 
		 Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB  |