1 / 9

Binary Search

Binary Search. Binary search. Given a key and sorted array a[] , find index i such 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.

hanh
Download Presentation

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