Skip to main content

Binary Search

Definition​

Binary search is an efficient algorithm used to find a target value within a sorted array or list. It works by repeatedly dividing the search interval in half until the target value is found or the interval is empty

Practice​

binarySearch(arr, target):
low = 0
high = length(arr) - 1

while low <= high:
mid = (low + high) / 2

if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1

return -1 // Target not found