Binary Search
Binary Search Function in Python
def binary_search(nums, target):
mid_val = len(nums)//2
print("===============================")
print("mid_index is ", mid_val)
print("len of nums is ", len(nums))
print(nums)
print(target)
if target == nums[mid_val]:
print("Found match value!")
return nums[mid_val]
elif len(nums) == 1:
print("Found closest value!")
return nums
elif target < nums[mid_val]:
binary_search(nums[:mid_val],target)
else:
binary_search(nums[mid_val:],target)
num_list = [1,4,6,7,9,10,11,12,15,20,24,59,73,88]
result = binary_search(num_list,12)
print(result)
result = binary_search(num_list,5)
print(result)