Skip to main content

Binary Search Fundamentals

Essential concepts and techniques for implementing binary search algorithms.

Search in sorted array

  • Basic Binary Search: Find target element in sorted array
  • Leftmost Position: Find first occurrence of target
  • Rightmost Position: Find last occurrence of target
  • Insert Position: Find position to insert target element

Search space reduction

  • Halving Search Space: Reduce search space by half each iteration
  • O(log n) Complexity: Logarithmic time complexity
  • Space Efficiency: Constant space complexity
  • Convergence: Guaranteed to find solution or determine absence

Boundary conditions

  • Left and Right Pointers: Managing search boundaries
  • Integer Overflow: Handling large numbers in midpoint calculation
  • Empty Array: Handling edge case of empty input
  • Single Element: Handling array with one element

Implementation variations

  • Recursive Implementation: Using recursion for binary search
  • Iterative Implementation: Using loops for binary search
  • Template Approach: Generic template for binary search problems
  • Custom Comparison: Using custom comparison functions

Search in rotated array

  • Rotated Sorted Array: Array rotated at unknown pivot
  • Find Pivot: Locate rotation point
  • Search in Rotated Array: Find target in rotated array
  • Minimum in Rotated Array: Find minimum element