Hide sidebar

Intro to Binary Search

Binary search is a highly efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing the search interval in half. The core idea is to use a 'divide and conquer' strategy to eliminate half of the remaining elements in each step.

The process begins by comparing the target value with the middle element of the array. If the target value matches the middle element, its position in the array is returned. If the target value is less than the middle element, the search continues in the lower half of the array. If the target value is greater than the middle element, the search continues in the upper half of the array.

This process is repeated, narrowing down the search space by half each time, until the target value is found or the search interval is empty. This efficiency is what makes binary search a cornerstone algorithm in computer science, with a time complexity of O(log n).

Binary Search Visualization

Step 1 of 0
Select Target Value:
Target Value
23
Left Pointer
None
Mid Pointer
None
Right Pointer
None
Current Action
Ready to start binary search
25812162338455667788999
Comparisons: 0
Max Comparisons for 13 elements: 4
Status: Searching...
Target Found
Mid Pointer (M)
Left Pointer (L)
Right Pointer (R)
Active Search Range
Eliminated
Divide and Conquer Search: Binary search repeatedly divides the sorted array in half, eliminating the half that cannot contain the target. Time complexity: O(log n), Space complexity: O(1) for iterative implementations.