hanh
Uploaded by
9 SLIDES
296 VUES
90LIKES

Binary Search

DESCRIPTION

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.

1 / 9

Download Presentation
Télécharger la présentation

Binary Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

More Related
SlideServe
Audio
Live Player
Audio Wave
Play slide audio to activate visualizer