Docsity

Binary Search Algorithm: Finding an Index in a Sorted Array, Slides of Data Representation and Algorithm Design

Partial preview of the text

Download Binary Search Algorithm: Finding an Index in a Sorted Array and more Slides Data Representation and Algorithm Design in PDF only on Docsity!

lo

Binary search. such that a[i] =Given value value, or report that no such index exists. and sorted array a[], find index i

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

hi

Binary search. such that a[i] =Given value value, or report that no such index exists. and sorted array a[], find index i

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

lo^60 131 142253 334 435 516 mid^537 648729 84109311 9512 961397 hi^14

Binary search. such that a[i] =Given value value, or report that no such index exists. and sorted array a[], find index i

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

lo^60 131 142 mid^253 334 435 51 hi^6537 648729 84109311 9512

Binary search. such that a[i] =Given value value, or report that no such index exists. and sorted array a[], find index i

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

(^60131 142253 33) lo (^4 435 51) hi 6 537 648 729 8410 9311 9512 9613 9714

Binary search. such that a[i] =Given value value, or report that no such index exists. and sorted array a[], find index i

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

(^60131 142253 33) lo 4 435 516 537 648 729 8410 9311 9512 9613 9714 hi

Binary search. such that a[i] =Given value value, or report that no such index exists. and sorted array a[], find index i

Invariant. Algorithm maintains a[lo]  value  a[hi].

Ex. Binary search for 33.

(^60131 142253 33) lo 4 435 516 537 648 729 8410 9311 9512 9613 9714 midhi