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

An explanation of the binary search algorithm, which is used to find the index of a specific value in a sorted array. The algorithm maintains the invariant that the value to be found is between the first and last elements of the array, and repeatedly divides the search interval in half. The process continues until the value is found or the interval is empty.

Typology: Slides

2011/2012

Uploaded on 07/15/2012

saandeep
saandeep 🇮🇳

4.5

(6)

99 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
8 2 1 3 4 6 5 7 10 9 11 12 14 13 0
64 14 13 25 33 51 43 53 84 72 93 95 97 96 6
Binary Search
lo
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
Ex. Binary search for 33.
hi
docsity.com
pf3
pf4
pf5
pf8
pf9

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