Вы находитесь на странице: 1из 3

2.

Binary Search
This is used for searching in a sorted array. Test the middle element of
the array. If it is too big. Repeat the process in the left half of the array, and
the right half if its too small. In this way, the amount of space that needs to be
searched is halved every time, so the time is O(log n).
Binary Search Algorithm Data Structure
efore we reading through inary search algorithm, let us recap se!uential
search or linear search. In "inear search algorithm searching begins with
searching every element of the list till the re!uired record is found. #lso it
doesnt demand the se!uence or order of elements in the list. If the list is !uite
huge, then this approach is not optimal. The drawbac$s of se!uential search
can be eliminated by using inary search algorithm.
How binary search works?
In binary search, it halves the si%e of the list to search in each iterations. It is
faster then "inear search. inary search re!uires sorted data to operate on
since the data may not be contiguous li$e the pages of a boo$. &e cannot
guess which !uarter of the data the re!uired item may be in. 'o we divide the
list in the center each time.
Example Search !or page number " in a book o! 2# pages
Typical e(ample of binary search is searching for a page in a boo$. 'uppose
you were searching for page ) in boo$ of *+ pages. ,ou would first open it at
random towards the later half of the boo$. If the page is less than ), you
would open at a page to the right, it is greater than ) you would open at a page
to the left, repeating the process till page ) was found. #s you can see, by the
first instinctive search, you dramatically reduced the number of pages to
search.
In the above e(ample, we have an sorted list of si%e *+. The first comparison
is with the middle element -age number .+. This eliminates the last
.+ elements, as -age ) is less then .+. The second comparison is with the
middle element from -age . to -age .+, i.e. with -age /. This eliminates
-age . to -age / as -age ) is greater then -age /. This continues until -age )
is found. 0ow let us formulate the algorithm for binary search.
Algorithm Binary Search
This represents the binary search method to find a re!uired item in a list
sorted in increasing order .
I0-1T2 'orted "I'T of si%e 0, Target 3alue T
O1T-1T2 -osition of T in the "I'T 4 I
56I0
.. 7#8 4 0
7I0 4 .
9O10: 4 false
*. &;I"5 (9O10: is false) and (7#8 < 4 7I0)
*.. 7I: 4 (7#8 = 7I0):I3 *
*.* If T 4 "I'T >7I:?
I47I:
9O10: 4 true
5lse If T @ "I'T>7I:?
7#8 4 7I:A.
5lse
7I0 4 7:=.
50:
Analysis o! Binary Search
The binary search method needs noB more than >Iog*n? = . comparisons. This
implies that for an array of a million entries, only about twenty comparisons
will be needed. Contrast this with the case of se!uential search which on the
average will need (n=.)D* comparisons.
In the binary search method Eust described above, it is always the $ey in the
middle of the list currently being e(amined that is used for comparison. The
splitting of the list can be illustrated through a binary decision tree in which
the value of a node is the inde( of the $ey being tested.
'uppose there are F. records, then the first $ey compared is at location .) of
the list since (. = F.)D* 4 .). If the $ey is less than the $ey at location .) the
location G is tested since (. = ./)D* 4 GB or if $ey is less than the $ey at
location .), then the location *H is tested.
Example $rogram to search !or an item using Binary Search
.
*
F
H
/
)
I
G
J
.+
..
.*
.F
.H
./
.)
.I
.G
.J
*+
*.
**
*F
*H
*/
*)
*I
*G
*J
F+
F.
F*
FF
FH
F/
F)
FI
FG
FJ
H+
Kinclude @stdio.h<

int main()
L
int c, first, last, middle, n, search, array>.++?B

printf(M5nter number of elementsNnM)B
scanf(MOdM,Pn)B

printf(M5nter Od integersNnM, n)B

for ( c 4 + B c @ n B c== )
scanf(MOdM,Parray>c?)B

printf(M5nter value to findNnM)B
scanf(MOdM,Psearch)B

first 4 +B
last 4 n A .B
middle 4 (first=last)D*B

while( first @4 last )
L
if ( array>middle? @ search )
first 4 middle = .B
else if ( array>middle? 44 search )
L
printf(MOd found at location Od.NnM, search, middle=.)B
brea$B
Q
else
last 4 middle A .B

middle 4 (first = last)D*B
Q
if ( first < last )
printf(M0ot foundR Od is not present in the list.NnM, search)B

return +B
Q

Вам также может понравиться