Binary Search
Definition:
Binary search is an efficient algorithm for finding an element in a sorted array. It works by repeatedly dividing the search interval in half. If the target value is less than the middle element, the search continues in the left half; if the target is greater, the search continues in the right half. This process continues until the element is found or the search interval becomes empty.
Characteristics:
-
Divide and Conquer:
- Binary search applies the divide-and-conquer strategy by repeatedly halving the search space until the target element is found or the subarray is empty.
-
Efficient for Sorted Data:
- Binary search requires the input array to be sorted. It is highly efficient for large datasets compared to linear search.
-
Works on Indexable Structures:
- Binary search is best suited for data structures that allow random access, such as arrays, but not for linked lists, where traversal is sequential.
-
Non-Adaptive:
- Binary search does not adjust to find elements more quickly when there are patterns or duplicate elements in the array, unlike some adaptive algorithms.
Video Explanation
