What is the time complexity of the binary search algorithm?

In summary, the time complexity of BinarySearch is O(log n) where $n$ is the number of elements in the array.
  • #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)
 
Technology news on Phys.org
  • #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
1
Views
1K
Replies
1
Views
1K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
1
Views
835
Replies
30
Views
5K
Back
Top