Binary Search VisualizerO(log N) Logarithmic Search

LeetCode #704
Target Value42
Comparisons0
Search StatusSEARCHING...
Step 1 / 0
Ready to search.
LOW
4
[0]
9
[1]
15
[2]
23
[3]
31
[4]
42
[5]
56
[6]
68
[7]
77
[8]
89
[9]
HIGH
95
[10]
Target:
Code Execution

💡 Binary Search in Layman's Terms

Binary Search is like opening a physical dictionary right in the middle! Because the array is already sorted, every single comparison cuts your search space in half.

How it Works Step-by-Step:
  • 1Look Right at the Middle: Calculate the exact middle index (mid) between your left and right bounds.
  • 2Compare with Target:
    • If middle value equals target âž” Target Found! 🎉
    • If middle value is smaller âž” Throw away the left half (low = mid + 1).
    • If middle value is larger âž” Throw away the right half (high = mid - 1).
  • 3Repeat Until Found: Keep halving the search space until you find the target or run out of numbers (low > high).
🎯 Real-World Analogy: The 1-to-100 Number Guessing Game

Imagine guessing a secret number between 1 and 100. If your first guess is 50 and the host says "Too Low!", you instantly eliminate numbers 1 through 50! Your next guess is 75 (middle of 51 to 100). By always guessing the middle, you can find any number out of 100 in just **7 guesses or fewer**!

State Variables
low0
mid-
high10
arr[mid]-
target42
Time: O(log N)Space: O(1)
LeetCode #704