2,642 views
2 votes
2 votes
for binary search in an array of n elements the average number of searches is $\left \lfloor \log_{2}n \right \rfloor$ or $\left \lceil \log_{2}n \right \rceil$ ?

4 Answers

1 votes
1 votes

let there are 15 elements

 

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
value at the  i+010 i+092 i+432 i+487 i+532 i+576 i+623 i+654 i+655 i+709 i+713 i+717 i+786 i+876 i+901
no.of comparissions 4 3 4 2 ( finds at 2nd comparission) 4 3 4 1 ( finds at 1st comparission) 4 3 4 2 ( finds at 2nd comparission) 4 3 4

 

Avg.case = ∑ ( probability for jth index * no.of comparissions for jth index )

               = ( P(j=1) * no.of comparissions for value at (1) ) + ( P(j=2) * no.of comparissions for value at (2) ) +....+ ( P(j=15) * no.of comparissions for value at (15) )

probability of jth index = $\frac{1}{n}$  (due to equal probability)

Avg.case = P(j) * ( ∑ ( no.of comparissions for jth index ) )

              = $\frac{1}{15}$  * ( 20 * 1 + 21 * 2 + 22 * 3 + 23 *4  ) =  $\frac{1}{15}$  * ( 1+4+12+32  ) =  $\frac{1}{15}$  * ( 49) -----> which gives 3.2

i can't decide it is floor or ceil

 

let take no.of elements 11

  1 2 3 4 5 6 7 8 9 10 11
value at the  i+010 i+092 i+432 i+487 i+532 i+576 i+623 i+654 i+655 i+709 i+713
no.of comparissions 3 4 2 ( finds at 2nd comparission) 3 4 1 ( finds at 1st comparission) 3 4 2 ( finds at 2nd comparission) 3 4

 

Avg.case = ∑ ( probability for jth index * no.of comparissions for jth index )

               = ( P(j=1) * no.of comparissions for value at (1) ) + ( P(j=2) * no.of comparissions for value at (2) ) +....+ ( P(j=11) * no.of comparissions for value at (15) )

probability of jth index = $\frac{1}{n}$  (due to equal probability)

Avg.case = P(j) * ( ∑ ( no.of comparissions for jth index ) )

              = $\frac{1}{11}$  * ( 20 * 1 + 21 * 2 + 22 * 3 + 22 *4  ) =  $\frac{1}{11}$  * ( 1+4+12+16  ) =  $\frac{1}{11}$  * ( 33 ) -----> which gives 3

but if we take ⌈ log2 11 ⌉ = 4 ===>  if we take ⌊ log2 11 ⌋  = 3

 

( it is my version on taking floor, may be i am wrong, please comment the correct reason if you think i did wrong )

0 votes
0 votes

I'm getting floor value. pls tell me if my approach is wrong

Related questions

0 votes
0 votes
2 answers
1
dhruba asked Jun 5, 2023
1,163 views
Binary search is performed on a sorted array of n elements. The search key is not in the array and falls between the elements at positions m and m+1 (where 1 ≤ m < n). ...
1 votes
1 votes
4 answers
2
Abhishek Kumar 38 asked Dec 15, 2018
2,125 views
There are two sorted list each of length n. An element to be searched in the both the lists. The lists are mutually exclusive. The maximum number of comparisons required ...
0 votes
0 votes
1 answer
3
Sabir Khan asked Aug 8, 2018
1,092 views
The minimum number of comparisons required to determine if an integer appears more than n/2 times in a sorted array of n integers is(A) (n)(B) (logn)(C) (log*n)(D) (1)
0 votes
0 votes
1 answer
4
shweta sah asked Jun 22, 2018
837 views
Find the average number of comparisons in a binary search on a sorted array of 10 consecutive integers starting from 1.1) 2.62)2.73)2.84)2.9