2
$\begingroup$

I know this question is very trivial to ask, but I have got some doubt while solving this problem.Code is given below

public class BinarySearch {
public static int binSearch(int a[], int x) { // a is sorted
int low = 0, high = a.length - 1;
while (low <= high) {
 int mid = (low + high) / 2;
 if (x < a[mid])
 high = mid - 1;
 else if (x > a[mid])
 low = mid + 1;
 else
 return mid;
}
return Integer.MIN_VALUE;
}

If we want to calculate the worst case comparisons of binary search,then

it will be 2ドル \log n+1,ドル because $\log n+1$ for checking (low<=high) and $\log n$ for (x < a[mid]) or if (x > a[mid]).

Alright , i have no issue here , i can happily say that worst case comparison for Binary search is 2ドル\log n +1$


I faced issue when i tried to solve the recurrence equation, given as-:

$$T(n)=T(\frac{n}{2})+1$$

$\text{Base Case},円,円T(1)=1$

$T(\frac{n}{2})=T(\frac{n}{4})+1$

Generalizing the equation i got-:

$T(n)=T(\frac{n}{2^{k}})+1+1..+1(k \text{times})$

$T(n)=T(\frac{n}{2^{k}})+k$

$\frac{n}{2^k}=1$

$k=\log n$

So $T(n)=\log n+1$ What actually this value signifies? Is it Minimum number of comparison? Confused Please help me out

asked Oct 26, 2017 at 7:16
$\endgroup$
4
  • $\begingroup$ Are you asking about the meaning of the recurrence in relation to binary search? $\endgroup$ Commented Oct 26, 2017 at 8:21
  • $\begingroup$ You first spent time explaining that the number of comparisons in each recursive step is 2ドル$ but then you use a recurrence pattern where you set it to 1ドル$. $\endgroup$ Commented Oct 26, 2017 at 8:25
  • $\begingroup$ @adrianN yes exactly.I want to know that after solving the recurrence relation for binary search , then what should it give?it should give the number of comparisons right? but as explained above ,it's not matching $\endgroup$ Commented Oct 26, 2017 at 9:22
  • $\begingroup$ @adrianN can you please help me out ? $\endgroup$ Commented Oct 26, 2017 at 11:40

4 Answers 4

2
$\begingroup$

The value that you've got is the number of searches required in the worst case by binary search. Each function call is responsible for only 1 search. And your recurrence relation justifies that. So the recurrence relation you wrote is for the number of searches. Indeed we require $\log n +1$ searches if it's a worst case. Take any $n$, say $n=7,8$, and try and see for yourself.

Yuval Filmus
281k27 gold badges317 silver badges514 bronze badges
answered May 1, 2021 at 13:16
$\endgroup$
1
$\begingroup$

The recurrence is supposed to express the worst-case runtime of binary search on an input of size $n$ (the letter $T$ might stand for time). The range you have to search search is halved in each step, so you get the $T(n/2)$ term on the right side.

Most of the time people only count the comparisons of the data, not the extra comparison for the loop condition, so it takes 1 comparison to halve the search space. For your analysis you might want to write a $+2$ instead of the $+1,ドル since you do count the loop condition.

As we want to express the worst case runtime, we assume that the element is not in the array, so the recursion terminates only when there is just one element left. This provides us with the base case of $T(1)=1$.

answered Oct 26, 2017 at 12:15
$\endgroup$
0
$\begingroup$

It's an upper bound on the number of comparisons. It's not lower bound because in the best case, you find $x$ immediately (its the middle element), but in the worst case, $x$ isn't in the array, so you had to dive down till the base case in the recursion.

answered Oct 26, 2017 at 9:18
$\endgroup$
0
$\begingroup$

@laura the 2logn+1 is the number of search comparisons happening in the worst case.

But the recurrence relation that you are using i.e. T(n) = T(n/2) + 1 is the Worst-case Time complexity recurrence relation of the Binary Search and not the number of searches that are being done in binary search in the worst case.

Both notions are different things the first one talks about the comparisons in worst case and the second one talks about the running time in worst case.

answered Sep 2, 2021 at 11:41
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.