def binary_search(nums, low, high, target): if (low > high): return -1 # Finding the mid using integer division mid = low + (high - low) // 2 # Target value is present at the middle of the array if nums[mid] == target: return mid # If the target value is greater than the middle, ignore the first half elif nums[mid] < target: return binary_search(nums, mid + 1, high, target) # If the target value is less than the middle, ignore the second half return binary_search(nums, low, mid - 1, target) def main(): nums_list= [[1, 2, 3, 4, 5, 6, 7], [10, 20, 30, 40, 50, 60], [12, 24, 35, 47, 58, 69, 72, 83, 94], [5, 13, 28, 41, 56, 63, 77, 82, 99, 105], [5, 7, 12, 17, 21, 28, 33, 37, 41, 48, 52, 57, 62, 68, 72]] target_list = [1, 50, 12, 56, 5] for i in range(len(target_list)): print((i + 1), ".\tSorted array: ", nums_list[i], "\n\ttarget", target_list[i], "found at index ", \ binary_search(nums_list[i], 0, len(nums_list[i]) - 1, target_list[i])) print("-"*100) if __name__ == '__main__': main()