Searching

Binary Search

Target:
Size:14
Visualizer is loading, please wait...
Execution Narrative
Step 1 of 3Start
Line 1
Starting Binary Search for target 55 within initial range [0..13].
Comparisons0
Operations0

Overview & Concept

Binary Search is an optimal divide-and-conquer search algorithm for sorted arrays. By comparing the search target with the middle element, it eliminates half of the remaining search space in each step, achieving guaranteed O(log n) logarithmic time complexity.

How It Works

Pre-condition: Requires array to be sorted in ascending order.

Midpoint Calculation: Computes mid = low + ⌊(high - low) / 2⌋ (safeguarding against integer overflow).

Three-Way Comparison: If A[mid] == target, returns mid; if A[mid] < target, discards left half; if A[mid] > target, discards right half.

Logarithmic Reduction: Search interval halves in each iteration until low > high.

Step-by-Step Execution

1. Initialize PointersSet low = 0 and high = n - 1.
2. Compute MidpointCalculate mid = Math.floor((low + high) / 2).
3. Compare & HalveIf A[mid] == target, return mid. If A[mid] < target, set low = mid + 1. If A[mid] > target, set high = mid - 1.
4. Termination CheckRepeat while low ≤ high. If low > high, target is absent; return -1.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(1)O(log n)O(log n)O(1)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
procedure binarySearch(A, target):
  low = 0; high = length(A) - 1
  while low <= high:
    mid = floor((low + high) / 2)
    if A[mid] == target: return mid
    else if A[mid] < target:
      low = mid + 1 // eliminate left half
    else:
      high = mid - 1 // eliminate right half
  return -1 // target not found
Algorithmic Invariants
  • Search Interval Invariant: If target exists in array, it is strictly within A[low..high].
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Logarithmic O(log n) time: can search 1,000,000,000 elements in at most 30 comparisons.
  • •O(1) auxiliary space in iterative implementation.

Trade-offs & Limitations

  • •Requires pre-sorted array: sorting an unsorted array takes O(n log n).
  • •Requires random access: inefficient on linked lists.

Real-World Applications

Database Index B-Tree Searching

Searching sorted keys inside B-Tree disk pages.

Git Bisect

Binary searching commit history to isolate the commit that introduced a regression.

Numeric Optimization & Root Finding

Bisection method solving continuous mathematical equations.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When searching sorted collections with random index access.
  • ✓When performing repeated queries on static or infrequently mutated datasets.

Consider Alternatives When

  • ✕When the dataset is unsorted and queried only once.
  • ✕When data is stored in a linked list without O(1) random access.

Compare trade-offs and complexity across complementary tools in AlgoViz.