Divide-N-Conquer - Binary Search
Divide-N-Conquer - Binary Search
-
T(n)I 1
JW~| 2T(v^) + logn
~4
n>4
H
T(n) \"
{ 2T(v^) + ^ n
n
<4
>4
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
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
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.
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
the range doesnot affect the outcomeof the search.If low becomesgreater
than high, then x is not presentand hencethe loopis exited. \342\226\241
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
A;
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
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)
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,