An interactive step-by-step visualisation of the Binary Search pattern. Watch how the search space is halved with each comparison, achieving O(log n) time complexity.
1def binary_search(nums: list[int], target: int) -> int:2 left = 03 right = len(nums) - 145 while left <= right:6 mid = (left + right) // 278 if nums[mid] == target:9 return mid10 elif nums[mid] < target:11 left = mid + 112 else:13 right = mid - 11415 return -1
We have a sorted array [1, 3, 5, 7, 9, 11, 13] and need to find the index of target value 9.