Tracing Binary Search

COSC 2306: Data Programming - Search Algorithm Visualizer

Iterative
Recursive

Execution Trace

Select a target word and click "Step" to begin tracing.

Controls

# Binary Search visualizer ready.
# Mode: Iterative
Python Implementation (Iterative)
def binary_search(arr, target):
    low = 0
    high = len(arr) - 1
    
    while low <= high:
        mid = (low + high) // 2
        
        if arr[mid] == target:
            return mid
        elif target < arr[mid]:
            high = mid - 1  # Search left half
        else:
            low = mid + 1   # Search right half
            
    return -1