Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many comparisons will binary search make in the worst case using this algorithm?

Tags:

Hi there below is the pseudo code for my binary search implementation:

Input: (A[0...n-1], K)
begin
   l ← 0; r ← n-1
   while l ≤ r do
      m ← floor((l+r)/2)
      if K > A[m] then l ← m+1
      else if K < A[m] then r ← m-1 else return m
      end if 
   end while
   return -1 // key not found
end

I was just wondering how to calculate the number of comparisons this implementation would make in the worst case for a sorted array of size n?

Would the number of comparisons = lg n + 1? or something different?

like image 991
Harry Tiron Avatar asked May 13 '12 10:05

Harry Tiron


People also ask

How many comparisons are there in a binary search worst case?

In general, for a list of length N, the worst case is N comparisons and the average case is N/2 comparisons.

How many comparisons does binary search make?

Answer and Explanation: For a sorted list of 1024 elements, a binary search takes at most 11 comparisons. Normally, the binary search algorithm works with half of the array on each pass.

How many comparisons are needed in worst case?

The worst case requires an average of 1/2 N comparisons per pass and occurs when the list is presorted in reverse order.

How many number of comparisons will be done in worst case using binary search if the number of elements in the array are 32?

Here, we have to apply the binary search on 32 elements. So, it will take log232 = 5 comparisons to search for the element.


2 Answers

A very minor correction to hielsnoppe's answer:

In an n-element array (n > 0), the element to compare is at index m = floor((n-1)/2). So there are three possibilities

  1. A[m] < K, then after one comparison, the search continues in an n-1-m = ceiling((n-1)/2)-element array.
  2. A[m] > K, then after two comparisons, the search continues in an m-element array.
  3. A[m] == K, then we're done after two comparisons.

So if we denote the maximal (worst-case) number of comparisons for a search in an n-element array by C(n), we have

C(0) = 0
C(n) = max { 1 + C(ceiling((n-1)/2), 2 + C(floor((n-1)/2) }, n > 0

For odd n = 2k+1, the floor and ceiling are identical, so the maximum is evidently the latter,

C(2k+1) = 2 + C(k)

and for even n = 2k, we find

C(2k) = max { 1 + C(k), 2 + C(k-1) }.

For n = 2, that resolves to C(2) = 1 + C(1) = 1 + 2 = 3, for all larger even n, the maximum is 2 + C(k-1), since for n >= 1 we have C(n) <= C(n+1) <= C(n) + 1.

Evaluating the recursion for the first few n, we find

C(0) = 0
C(1) = 2
C(2) = 3
C(3) = C(4) = 4
C(5) = C(6) = 5
C(7) = C(8) = C(9) = C(10) = 6
C(11) = ... = C(14) = 7
C(15) = ... = C(22) = 8
C(23) = ... = C(30) = 9

So by induction we prove

C(n) = 2k, if 2^k <= n+1 < 2k + 2^(k-1), and
C(n) = 2k+1, if 2^k + 2^(k-1) <= n+1 < 2^(k+1)

or

C(n) = 2*log2(n+1) + floor(2*(n+1)/(3*2^floor(log2(n+1)))).

This is an exact upper bound.

like image 41
Daniel Fischer Avatar answered Sep 19 '22 05:09

Daniel Fischer


The worst-case in this case is, if the element K is not present in A and smaller than all elements in A. Then we have two comparisons in each step: K > A[m] and K < A[m].

For in each step the array is being cut into two parts, each of the size (n-1)/2, we have a maximum of log_2(n-1) steps.

This leads to a total of 2*log_2(n-1) comparisons, which asymptotically indeed equals to O(log(n)).

like image 78
hielsnoppe Avatar answered Sep 20 '22 05:09

hielsnoppe