0% found this document useful (0 votes)
6 views11 pages

DS Lec07

The document describes the binary search algorithm, which efficiently finds the index of a specified value in a sorted array. It outlines the algorithm's process, including maintaining the invariant that the target value lies between the current low and high indices. An example is provided, demonstrating the search for the value 33 within a given sorted array.

Uploaded by

tabmoy76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

DS Lec07

The document describes the binary search algorithm, which efficiently finds the index of a specified value in a sorted array. It outlines the algorithm's process, including maintaining the invariant that the target value lies between the current low and high indices. An example is provided, demonstrating the search for the value 33 within a given sorted array.

Uploaded by

tabmoy76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

BINARY SEARCH

Binary Search Algorithm

function binary_search(A, n, T) is
L := 0
R := n − 1
while L ≤ R do
m := floor((L + R) / 2)
if A[m] < T then
L := m + 1
else if A[m] > T then
R := m − 1
else:
return m
return unsuccessful
Binary Search

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.

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

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

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

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

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

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

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

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
mid
Binary Search

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.

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
mid

You might also like