Live Pseudocode
Highlighted line matches the current step.
BinarySearch(A, target):
low = 0, high = n-1
while low <= high:
mid = β(low + high)/2β
if A[mid] == target: return mid
else if A[mid] < target: low = mid + 1
else: high = mid - 1
return NOT_FOUND
Key idea: halving the search space every step β O(log n).
Complexity (Quick Visual)
Max steps (worst case)
Steps β 5 (about logβ(n))
Time: O(log n)
Space: Iterative O(1), Recursive O(log n)
Quick Look
Common mistakes
β’ Using it on an unsorted array β
β’ Wrong loop condition: use low β€ high
β’ Wrong updates: low=mid+1 and high=midβ1