def binarySearch(array, value):
min_number = 0
max_number = len(array) - 1
while min_number <= max_number:
mid_number = (min_number + max_number) // 2
if value == array[mid_number]:
return f"Found {value} at Index {mid_number}"
elif value < array[mid_number]:
max_number = mid_number - 1
else:
min_number = mid_number + 1
return "Not Found"
array = [0, 2, 5, 8, 10, 12, 15]
print(binarySearch(array, 12))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter