Binary Search
A logarithmic-time search algorithm for sorted arrays.
Time Complexity
O(log n)
Very FastSpace Complexity
O(1)
ImmediateIn-Place
Yes
YesStability
Yes
YesDifficulty
Easy
★★☆☆☆Description
Binary Search is an efficient algorithm for finding an element in a sorted array.
It works by repeatedly dividing the search interval in half:
- Compare the target with the middle element
- If equal, return the index
- If less, repeat on the left half
- If greater, repeat on the right half
It is extremely fast with a time complexity of O(log n)
but only applicable to sorted collections.