What is the time complexity of the binary search algorithm?

Click For Summary

Discussion Overview

The discussion revolves around determining the time complexity of the binary search algorithm, focusing on its theoretical aspects and specific cases. Participants explore the recurrence relations, worst-case scenarios, and the implications of different input sizes.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Some participants propose a recurrence relation for the time complexity as \( T(n) = T(\frac{n}{2}) + C \), where \( C \) represents constant time operations.
  • Others argue that the worst-case time complexity is \( O(\log n) \) based on the recurrence relation derived.
  • Several participants question the time complexity when \( N = 0 \) and \( N = 1 \), suggesting that the costs associated with these cases should be considered.
  • Some participants calculate specific costs for \( N = 0 \) and \( N = 1 \), leading to discussions about whether these represent worst-case scenarios.
  • There is a challenge regarding the assumption that the value being searched is present in the array, which affects the time complexity calculations.
  • Participants discuss the implications of calling the binary search with different ranges, such as \( i = BinarySearch(A, value, 3, 4) \), and how this affects the complexity.
  • There is uncertainty about the representation of \( n \) in different contexts and how it relates to the number of elements being searched through in specific recursions.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the general complexity of the binary search algorithm, as there are multiple competing views regarding specific cases and assumptions about input values.

Contextual Notes

Limitations include the dependence on the assumption that the value is present in the array, which affects the time complexity in edge cases. Additionally, the discussion highlights the need for clarity on what \( n \) represents in different contexts.

  • #31
evinda said:
Will the resursion-tree be like that?

View attachment 3390

If so, for which value of $n$ will it end? And how can I find the last value that it will take? (Thinking)

It would help if you specify at each level the fraction of $n$. (Worried)

At the top level you are calculating for $T(n)$.
One level down for $T(\lfloor\frac n 2\rfloor)$.
Etcetera, until we get to $T(0)$ for which the cost is $2$.

To get a sense of this, perhaps we can pick an example for say $n=8$. (Mmm)
And perhaps another one for $n=11$.
 
Technology news on Phys.org
  • #32
I like Serena said:
It would help if you specify at each level the fraction of $n$. (Worried)

At the top level you are calculating for $T(n)$.
One level down for $T(\lfloor\frac n 2\rfloor)$.
Etcetera, until we get to $T(0)$ for which the cost is $2$.

To get a sense of this, perhaps we can pick an example for say $n=8$. (Mmm)
And perhaps another one for $n=11$.

I am confused now.. (Sweating)

It is:

$$T(8)=T(4)+10=(T(2)+10)+10=T(1)+30=T(0)+40=12$$

$$T(11)=T(5)+10=T(2)+20=T(1)+30=12$$

But, how can could we make the recursion-tree? (Thinking)
 
  • #33
evinda said:
I am confused now.. (Sweating)

It is:

$$T(8)=T(4)+10=(T(2)+10)+10=T(1)+30=T(0)+40=12$$

$$T(11)=T(5)+10=T(2)+20=T(1)+30=12$$

But, how can could we make the recursion-tree? (Thinking)

Like this:
\begin{array}{|c|c|}
\hline
& \text{cost tree}\\
\hline
n=8 & 10 \\
& \downarrow \\
\lfloor n/2\rfloor =4 & 10 \\
& \downarrow \\
\lfloor \lfloor n/2\rfloor / 2\rfloor=\lfloor n/4\rfloor=2 & 10 \\
& \downarrow \\
\lfloor n/8\rfloor=1 & 10 \\
& \downarrow \\
\lfloor n/16\rfloor=0 & 2 \\
\hline
\text{Total }T(n) & 42 \\
\hline
\end{array}

How many times do we have $10$? (Wondering)
 
  • #34
I like Serena said:
Like this:
\begin{array}{|c|c|}
\hline
& \text{cost tree}\\
\hline
n=8 & 10 \\
& \downarrow \\
\lfloor n/2\rfloor =4 & 10 \\
& \downarrow \\
\lfloor \lfloor n/2\rfloor / 2\rfloor=\lfloor n/4\rfloor=2 & 10 \\
& \downarrow \\
\lfloor n/8\rfloor=1 & 10 \\
& \downarrow \\
\lfloor n/16\rfloor=0 & 2 \\
\hline
\text{Total }T(n) & 42 \\
\hline
\end{array}

How many times do we have $10$? (Wondering)

$$\lfloor \frac{8}{2}\rfloor \text{ times, right? }$$

(Thinking)
 
  • #35
evinda said:
$$\lfloor \frac{8}{2}\rfloor \text{ times, right? }$$

(Thinking)

Let's do it for $n=11$ as well:
\begin{array}{|c|c|}
\hline
& \text{cost tree}\\
\hline
n=11 & 10 \\
& \downarrow \\
\lfloor n/2\rfloor =5 & 10 \\
& \downarrow \\
\lfloor \lfloor n/2\rfloor / 2\rfloor=\lfloor n/4\rfloor=2 & 10 \\
& \downarrow \\
\lfloor n/8\rfloor=1 & 10 \\
& \downarrow \\
\lfloor n/16\rfloor=0 & 2 \\
\hline
\text{Total }T(n) & 42 \\
\hline
\end{array}

Is it $\lfloor \frac{11}{2}\rfloor$ times that we have $10$? (Wondering)
 
  • #36
I like Serena said:
Let's do it for $n=11$ as well:
\begin{array}{|c|c|}
\hline
& \text{cost tree}\\
\hline
n=11 & 10 \\
& \downarrow \\
\lfloor n/2\rfloor =5 & 10 \\
& \downarrow \\
\lfloor \lfloor n/2\rfloor / 2\rfloor=\lfloor n/4\rfloor=2 & 10 \\
& \downarrow \\
\lfloor n/8\rfloor=1 & 10 \\
& \downarrow \\
\lfloor n/16\rfloor=0 & 2 \\
\hline
\text{Total }T(n) & 42 \\
\hline
\end{array}

Is it $\lfloor \frac{11}{2}\rfloor$ times that we have $10$? (Wondering)

No, we have $4$ times cost $10$, and not $5$. (Worried)

How could we find a general formula? (Thinking)

Is it maybe:

$$\sum_{i=0}^{i-1} 10+2$$

(Thinking)
 
  • #37
evinda said:
No, we have $4$ times cost $10$, and not $5$. (Worried)

How could we find a general formula? (Thinking)

Is it maybe:

$$\sum_{i=0}^{i-1} 10+2$$

(Thinking)

That doesn't look like a proper formula! :eek:

Anyway, I'm off to sleep now. (Sleepy)
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
Replies
20
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 30 ·
2
Replies
30
Views
6K
  • · Replies 27 ·
Replies
27
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K