You are on page 1of 19

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

24 target > mid search in the right


else search in
left
③ if middle element ==
target element Hans
'

& 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@

-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

this may exceed


* m =
s(t④→ the int
range

/
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

target < middle right


S = m +1
?⃝
an =
&%?%ɧ3 , B
,
ie
,
20%3
if s -

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 .

If clement .< target

ri-intii.CO#*-6seI-=-tJeelImt > target


?⃝
÷÷¥÷:* →÷:÷
.
* Take
perform

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
?⃝

You might also like