diff --git a/03_recursion/python/05_binary_search_recursive.py b/03_recursion/python/05_binary_search_recursive.py index 3ed0b23..c6461d6 100644 --- a/03_recursion/python/05_binary_search_recursive.py +++ b/03_recursion/python/05_binary_search_recursive.py @@ -1,17 +1,20 @@ def binary_search(arr, target): - if not arr: - return -1 + if len(arr) == 0: + return None - low = 0 - high = len(arr) - 1 - mid = (low + high) // 2 + mid = len(arr) // 2 if arr[mid] == target: - return target + return mid elif arr[mid] > target: return binary_search(arr[:mid], target) else: - return binary_search(arr[mid+1:], target) + recursive_response = binary_search(arr[(mid + 1) :], target) + return ( + (mid + 1) + recursive_response + if recursive_response is not None + else recursive_response + ) print(binary_search([6, 7, 8, 9, 10], 8))