Binary Search
The binary search algorithm is a powerful technique used to find the index of a specified key within a sorted array. Given a sorted array `a[]` and a target `key`, the algorithm efficiently narrows down the search range based on the invariant that the `key` lies between the elements at indices `lo` and `hi`. If the key exists, it returns the index of the key; otherwise, it reports that the index does not exist. This method ensures logarithmic time complexity, making it suitable for large datasets.
Binary Search
E N D
Presentation Transcript
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohi
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohimid
Binary Search • Binary search. Given a key and sorted array a[], find index isuch that a[i] = key, or report that no such index exists. • Invariant. Algorithm maintains a[lo]key a[hi]. • Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohimid