Professional Documents
Culture Documents
Binary Search
Binary Search
⑤ =
[ -2,4 ,
9
,
10
,
12 , 14
,
18 ,
19 ]
ascending
-
an 2=1 19
,
12
,
6
,
S
,
3
,
2
,
-8
,
-
]
to
descending
-
order
maxwmparis.us : N .
No
of
.
elements
'
① find the middle element
& i z z En s 6 of
7 8
arr =
[ 2,4; 6
,
9
,⑧r ,
12 , 14
,
20
,
36,48 ]
targ
?⃝
?⃝
?⃝
s ÉÉ&q iiÉi%aEs%
d
=⑧ a
⑥Fi 36,48
d
⑦¥
nn
?⃝
.in?ns e:elematnotyovnd.L. Bcstcas@
0¥
-Why Binary Search ?
Find the
E- wax number
of
such
comparisons
in
worst case .
→en=÷⑨
I
* FINE :-O =
I
II. E- =¥=I⑧
↳
1¥ % '
¥00
Gerd is
.
•=!
?⃝
F- =/ N =
zk
log Cn) =
log (2)
log N =
klogz
K
-
Y.gg k=log①
Total
comparisons in the worst case -
log⑨
=
Search in
a 1
,
ooo
,
ooo
Binmysearulm.tl
lines
(
20conyomio-E.IO
=
11 better to
way find mid
/
2
ste-s-z-zst . s e/ - s--
* m =
s + ( e -
s )
s+⑤
# dsa with Kunal
⑨ lommclassroom
@ Kunal stat
Order Binary search
agnostic
Ñ=É%I%
tmgd target > middle :
left
c- -
m -
I
J increasing
else
decreasing
Snearo:¥hiyinMat¥¥•
riuestargct-q@i.H
Ans -
↳ ] & "
if an [ rJH=tm
=L.€¥retr
'
N*N =
N
0 @ )
-
* m
Poutworr@@
m*tn☒ & manner .
I
tayét=s7
%¥%÷* .
anip
f
"s
¢ase .
6
'
>
middle
2
BS
cold
on it
① If element target
↳ > 2 # forget ?
Hans
If element >
target
✗ Ignore it
rows
after
④ If element <
target
taz
:#É÷÷É→ * ②
the
at
i. e
mind
contains
[ 2,6 ]
lodywrane
the ans
MD
Consider the
four
④
①
-
(N ) +
fog ( parts
?⃝