‘Computer Science Stack Exchange isa Here's how it works:
question and answer site for students,
researchers and practitioners of
‘computer science. Join them; it only
takes a minute:
‘The best answers are
gnu ‘Anybody can ask ‘Anybody can voted up and rise tothe
‘a question ‘answer top
Proving that the average case complexity of binary search is O(log n)
| enow thatthe both the average and worst case complexly of binary search is Oog n) and | know how to prove the worst case complexlty
15 O(log n)using recurrence relations. But how woulé go about proving thatthe average case complexity of binary search is log ni?
augritins tre-ortenty aeweCse birerrs
ow cut an per bound arte wor case not bean upper boul forth weap sk?- ASC Oe 20
1 Answer
| think most text book will provide you a good proof
Fr me can show the average case complexity 2s follows
Assuming 2 uniform astibution ofthe position of the value that ane wants to fn in an
array of szen,
+ Forthe case of 1 rea the postion should be inthe middle so there sa probably of
forts case
«For the case of? reads, one will ead the middle poston and then ofthe 2 other
tal postions fom the 2 sub arrays. Tis probably is =
«Forth case af3 reads tere are 2+ 2 postions which resutn this cost asyou
Into the4subarays of the fst 2 subartays. Te probably fortis costs
+ Forthe case of reads, the probability for ths case Is
For the average case, the numberof reads willbe
Pale) an BO
Now you can do integration onan approximation formula which will give you O(n log(n))
otetnat "fe can be ctuted and bounded nto log) « 246 ~ nog)
‘Wis avery ao06 oy todo that applies to many cases
Another wayto seeit an alsobe 12" < log(n) #2
io) "80 gp
‘Then the formula above is bounded by “ES 2!
Te summation partis actually = 280) 1 = Lich is dently less
than n, multpying this with "82 gives you whatyou want log(n)
So you wil get the bound as you wans O(log(n))SU ormean
1 Gaara oan a cent vay oe open earn