0% found this document useful (0 votes)
8 views

Divide-N-Conquer - Binary Search

This document describes the binary search algorithm. It provides details on how binary search works by recursively dividing the search space in half at each step to look for a target value. Pseudocode is given for both recursive and iterative implementations of binary search.

Uploaded by

Zeenath
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)
8 views

Divide-N-Conquer - Binary Search

This document describes the binary search algorithm. It provides details on how binary search works by recursively dividing the search space in half at each step to look for a target value. Pseudocode is given for both recursive and iterative implementations of binary search.

Uploaded by

Zeenath
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/ 7

3.2.

BINARY SEARCH 131


(\302\2530

-
T(n)I 1
JW~| 2T(v^) + logn
~4
n>4
H

3.2 BINARY SEARCH


(\302\2531)

T(n) \"
{ 2T(v^) + ^ n
n
<4
>4

Let a,,1< i < n, bea list of elementsthat aresortedin nondecreasing order.


Considerthe problemof determiningwhethera given elementx is presentin
the list.If x is present,we are to determinea value j such that a.j = x. If a:
is not in the list,then j is to be set to zero.Let P = (n,a,i,..., at, x) denote
an arbitrary instanceof this searchproblem(n is the numberof elementsin
the list, a,i,...,
d( is the list of elements,and x is the elementsearchedfor).
Divide-and-conquer can be used to solve this problem.Let Small(P)be
true if n = 1.In this case,S(P)will take the value i if x = af, otherwiseit
will take the value 0. Then g(l) = 0(1).If P has morethan one element,it
can be divided (orreduced)into a new subproblem as follows. Pickan index
q (in the range [i,C]) and compare x with aq. Therearethree possibilities:
(1) x aq: In this casethe problemP is immediately solved. (2) x < aq:
\342\200\224

In this casex has to be searchedfor only in the sublist a2, aj+i,... ,a9_i.
There;fore,P reducesto (q i,a,i,.. ,aq-i,x). (3) x > aq: In this casethe
\342\200\224

\342\226\240

sublisl.to be searchedis a9+i,... , ag. P reducesto (I q, a9+i,..., ae,x). \342\200\224

In this example,any given problemP gets divided (reduced)into one


new subproblem.This division takes only 6(1)time. After a
with a9, the instanceremainingto be solved (if any) can be solved
comparison

by using this divide-and-conquer schemeagain. If q is always chosensuch


that aq is the middleelement(that is,q = + l)/2\\), then the \\_{n
searchalgorithmis known as binary search. Note that the answer to
resulting

the new subproblemis also the answer to the original problemP; there
is no need for any combining. Algorithm 3.2describesthis binary search
method,whereBinSrchhas four inputs a[ and x. It is initially invoked
],\302\253',/,

as BinSrch(a,l,n,x).
A nonrecursiveversion of BinSrchis given in Algorithm 3.3.BinSearch
has three inputs a,n,and x. The while loopcontinuesprocessing as long
as therearemoreelementsleft to check.At the conclusionof the procedure
0 is returned if x is not present,or j is returned,such that a\\j] = x.
Is BinSearchan algorithm? We must be sure that all of the operations
such as comparisons betweenx and a[mid\\ are well defined.The relational
operatorscarry out the comparisons amongelementsof a correctlyif these
operatorsare appropriately defined.DoesBinSearchterminate?We observe
132 CHAPTER3. DIVIDE-AND-CONQUER

1 AlgorithmBinSrch(a,i,l,x)
2 // Given an array a[i : /] of elementsin nondecreasing
3 // if returni <such
order,1< I, determinewhetherx is present,and
that x = a[j];elsereturn 0.
4 // so, j
5 {
6 if (/ = then // If Small(P)
\302\253)

7 {
8 if (x = a[i\\) thenreturni;
9 elsereturn0;
10 }
11 else
12 { // ReduceP into a smallersubproblem.
13 mid:=[(i+ l)/2\\;
14 if (x = a[mid])thenreturnmid;
15 elseif (x < a[mid\\) then
16 return mid 1,x);
BinSrch(a,\302\253',
\342\200\224

17 elsereturnBinSrch(a,mid + 1,1,x);
18 }
19 }

Algorithm3.2Recursivebinary search

1 AlgorithmBinSearch(a,n,x)
2 a[l:n] ofwelements
// Given nan>arraydetermine in nondecreasing
3 // order, 0, hetherx is present,and
4 // if so,return j such that x = a[j];elsereturn 0.
5 {
6 low := 1;high := n;
7 while (/ou;< /wg/i) do
8 {
9 mid := [(low+ high)/2j;
10 if (x < a[mid])thenhigh := 1; mid\342\200\224

11 elseif (a; > a[mic(|)thenlow :=mid+ 1;


12 elsereturnmid;
13 }
14 return0;
15 }

Algorithm3.3Iterativebinary search
3.2.BINARY SEARCH 133
that low and high are integervariablessuchthat eachtimethroughthe loop
eitherx is found or low is increasedby at leastone or high is decreasedby
at leastone.Thus we have two sequences of integersapproachingeachother
and eventually low becomesgreaterthan high and causesterminationin a
finite number of stepsif x is not present.

Example3.6Let us selectthe 14entries


-15,-6, 112,125,131,142,151
0,7, 9, 23,54, 82,101,
placethem in a[l: 14],and simulatethe stepsthat BinSearchgoesthrough
as it searchesfor different values of x. Only the variableslow, high, and
mid needto be tracedas we simulatethe algorithm.We try the following
values for x: 151, and 9 for two successfulsearchesand oneunsuccessful
\342\200\22414,

search.Table3.2shows the tracesof BinSearchon thesethreeinputs. \342\226\241

].r>i low high mid x= -14 low high mid


1 14 7 1 14 7
8 14 11 1
1
6 3
12 14 13 2 1
14 14 14 2 2 2
found 2 1 not found
x =9 low high mid
1 14 7
1 6 3
4 6 5
found

Table3.2Threeexamplesof binary searchon 14elements

give us a little moreconfidenceabout Algorithm


Theseexamplesmay
3.3, they by
but no means prove that it is correct.Proofsof algorithmsare
very useful becausethey establish the correctnessof the algorithmfor all
possible inputs, whereastesting gives much lessin the way of guarantees.
Unfortunately, algorithmproving is a very difficult processand the complete
proofof an algorithmcan be many timeslongerthan the algorithmitself.
We contentourselveswith an informal \"proof\" of BinSearch.

Theorem3.1Algorithm BinSearch(a,n,x)works correctly.


Proof:We assumethat allstatementswork as expectedand that
such as x > a[mid]are appropriatelycarriedout. Initially low = 1,
comparisons

high n, n > 0, and a[l]< a[2] < < a[n].If n = 0, the while loopis
\342\226\240-
\342\200\242
\342\200\242
\342\200\242
134 CHAPTER3. DIVIDE-AND-CONQUER

not enteredand 0 is returned.Otherwisewe observethat eachtimethrough


the loopthe possible
+ - - ...,
elementsto be checkedfor equality with x are a[low],
a[low 1], -, a[mid], a[high].If x = a[mid],then the algorithm
terminates successfully. Otherwisethe range is narrowed by eitherincreasing
low to mid + 1 or decreasinghigh to mid 1. Clearly this narrowingof \342\200\224

the range doesnot affect the outcomeof the search.If low becomesgreater
than high, then x is not presentand hencethe loopis exited. \342\226\241

Noticethat to fully test binary search,we neednot concernourselveswith


the values of a[l: n].By varying x sufficiently, we can observeall possible
computationsequences of BinSearchwithout devisingdifferent values for a.
To test all successfulsearches,x must takeon the n values in a. To test all
unsuccessfulsearches,x need only take on n + 1 different values.Thus the
complexity of testing BinSearchis 2n + 1 for eachn.
Now let'sanalyze the executionprofile of BinSearch.The two relevant
characteristics of this profile are the frequency countsand spacerequiredfor
the algorithm.For BinSearch,storageis requiredfor the n elementsof the
array plus the variableslow, high,mid, and x, or n + 4 locations. As for the
time, thereare three possibilities to consider:t he best,average,and worst
cases.
Supposewe begin by determiningthe time for BinSearchon the
data set. We observethat the only operationsin the algorithm are
previous

comparisons and somearithmeticand data movements.We concentrateon


comparisons betweenx and the elementsin a[ ], recognizingthat the
frequency count of all otheroperationsis of the sameorderas that for these
comparisons.Comparisons betweenx and elementsof a[ ] are referredto
as elementcomparisons. We assumethat only one comparison is neededto
determinewhich of the three possibilities of the if statementholds.The
numberof elementcomparisons neededto find eachof the 14elementsis

Elements:
Comparisons:
-15
3 42434143
[1] [2]
-6 [3]
0
[4]
7
[5]
9 [6]
23
[7]
54
[8]
82
[9]
101 112 [11]
[10]
4 2
[12] [13] [14]
125 131 142 151
4 3 4

No elementrequiresmorethan 4 comparisons to be found. The average


is obtainedby summingthe comparisons neededto find all 14itemsand
dividing by 14;this yields 45/14,or approximately3.21,comparisonsper
successfulsearchon the average.Thereare 15possible ways that an
searchmay terminatedependingon the value of x. If x < a[l],the
unsuccessful

algorithmrequires3 elementcomparisonsto determine that x is not present.


BinSearchrequires4 elementcomparisons.
Forallthe remainingpossibilities,
Thus the averagenumberof elementcomparisons for an unsuccessfulsearch
is (3 + 14* 4)/15= 59/15 3.93. \302\253

The analysis just done appliesto any sortedsequencecontaining14


elements. But the result we would preferis a formula for n elements.A good
3.2.MNARY SEARCH 135

way to derive such a formula plus a betterway to understandthe algorithm


is to considerthe sequence of values for mid that are producedby BinSearch
for all possible values of x. Thesevalues are nicely describedusinga binary
decisiontree in which the value in eachnodeis the value of mid. For
example, if n, 14,then Figure3.1containsa binary decisiontree that traces
\342\200\224

the way in which thesevalues are producedby BinSearch.

Figure3.1Binary decisiontree for binary search,n = 14


Thefirst comparison is x with a[7].If x < a[7],then the next comparison
is with similarly, if x > a[7], then the next comparison
\302\253,[3];
is with a[ll].
Each path through the tree representsa sequenceof comparisons in the
binary searchmethod. If x is present, then the algorithm will end at one
of the circularnodesthat liststhe index into the array wherex was found.
If x is not present,the algorithmwill terminateat one of the squarenodes.
Circularnodesarecalledinternalnodes, and squarenodesarereferredto as
externalnodes.
Theorem3.2If n is in the range[2fc~',2fc),then BinSearchmakesat most k
elementcomparisons for a successfulsearchand either 1or comparisons
\302\243;
\342\200\224

A;

for an unsuccessfulsearch.(In otherwords the timefor a successfulsearch


is O(logn) and for an unsuccessfulsearchis 9(logn)).

Proof:Considerthe binary decisiontree describingthe actionof BinSearch


on n elements.All successfulsearchesend at a circularnodewhereasall
unsuccessfulsearchesend at a square node.If 2fc_1 < n < 2fc, then all
circulaxnodesare at levels 1, 2,...,
k whereasall square nodesare at levels
136 CHAPTER3. DIVIDE-AND-CONQUER

k and k + 1 (note that the root is at level 1). The number of element
comparisons neededto terminateat a circularnodeon level i is i whereas
the numberof elementcomparisons neededto terminateat a squarenodeat
level i is only i 1.The theoremfollows.
\342\200\224
\342\226\241

Theorem3.2statesthe worst-case timefor binary search.To determine


the averagebehavior,we needto lookmorecloselyat the binary decisiontree
and equateits sizeto the numberof elementcomparisons in the algorithm.
Thedistanceof a nodefrom the root is one lessthan its level.The internal
path length I is the sum of the distancesof all internal nodesfrom the root.
Analogously, the externalpath length E is the sum of the distancesof all
externalnodesfrom the root.It is easy to show by inductionthat for any
binary tree with n internal nodes, E and / arerelatedby the formula
E = I + 2n
It turns out that there is a simplerelationshipbetweenE,I, and the
averagenumberof comparisons in binary search.Let As(n) be the average
numberof comparisons in a successfulsearch,and Au(n) the averagenumber
of comparisons in an unsuccessfulsearch.Thenumberof comparisons needed
to find an elementrepresentedby an internal nodeis one morethan the
distanceof this nodefrom the root.Hence,

As(n) = 1+I/n
The numberof comparisons on any path from the root to an externalnode
is equalto the distancebetweenthe rootand the externalnode.Sinceevery
binary tree with n internal nodeshas n + 1 externalnodes,it follows that

Au(n) = E/(n+ 1)

Usingthesethreeformulas for E,As(n),and Au(n), we find that

As(n) = (1+ 1/n)Au(n)-l


Promthis formulawe seethat As(n) and Au(n) are directly related.The
minimum value of As(n) (and hence Au(n)) is achievedby an algorithm
whosebinary decisiontree has minimum externaland internalpath length.
This minimum is achievedby the binary treeall of whoseexternalnodesare
on adjacentlevels,and this is preciselythe tree that is producedby binary
search.From Theorem3.2it follows that E is proportionalto n logn.Using
this in the precedingformulas, we concludethat As(n) and Au(n) are both
proportionalto log n. Thus we concludethat the average-and worst-case
numbersof comparisons for binary searchare the sameto within a constant
3.2.BINARY SEARCH 137

factor. The best-case analysis is easy. For a successfulsearchonly one


elementcomparisonis needed. For an unsuccessfulsearch,Theorem3.2
statesthat [logn\\ elementcomparisons areneededin the best case.
Inconclusion we are now able to completelydescribethe computingtime
of binary searchby giving formulas that describethe best,average, and
worst cases:
searches unsuccessful
successful searches
6(1), e(logn), G(logn) 6(logn)
best, average, worst best,average,worst
Can we expectanothersearchingalgorithmto be significantlybetterthan
binary searchin the worst case? This question is pursued rigorously in
Chapter10. But we can anticipatethe answer here, which is no. The
methodfor proving such an assertionis to view the binary decisiontree as
a generalmodelfor any searchingalgorithmthat dependson comparisons
of entireelements.Viewed in this way, we observethat the longestpath to
discover any elementis minimizedby binary search,and soany alternative
algorithmis no better from this point of view.
Beforewe end this section,there is an interestingvariation of binary
searchthat makesonly one comparisonper iterationof the while loop.
This variation appearsas Algorithm 3.4.Thecorrectness proofof this
variation is left as an exercise.

BinSearchwill sometimesmaketwice as many elementcomparisons as


BinSearchl(for example,when x > a[n\\). However, for successfulsearches
BinSearchlmay make(logn)/2moreelementcomparisons than BinSearch
(for example, when x = a[mid\\). The analysis of BinSearchli s left as an
exercise.It shouldbe easy to seethat the best-,average-,and worst-case times
for BinSearchlare 0(logn) for both successfuland unsuccessfulsearches.
Thesetwo algorithmswere run on a Sparc10/30. The first two rows in
Table3.3representthe averagetimefor a successfulsearch.The secondset
of two rows give the averagetimesfor all possible unsuccessfulsearches.For
both successfuland unsuccessfulsearchesBinSearchldid marginally better
than BinSearch.

EXERCISES
1.Run the recursiveand iterativeversions of binary searchand compare
the times. For appropriatesizesof n, have eachalgorithmfind every
clementin the set.Then try all n + 1possibleunsuccessfulsearches.
2. Proveby inductionthe relationshipE = / + 2n for a binary tree with
internalnodes.The variables E and / are the externaland internal
\342\226\240n,

path length, respectively.

You might also like