1

I tried to calculate the worst case of binary search (not binary search tree). My calculations: $$T(n) = T\left(\frac{n}{2}\right) + 1$$ $$T(n) = T\left(\frac{n}{4}\right) + (1+1) = T\left(\frac{n}{8}\right) + (1+1+1) = {\dots} = T\left(\frac{n}{2^{k}}\right)+(1\cdot k) $$ $$T(n)=T(1) + (1\cdot k) = c_{1} + (1\cdot k) = c_{1} + log_{2}n = c_{1}+\frac{log(n)}{log(2)} $$ Finally the complexity should be $$O(log(n)) $$ Is this a good way to prove the worst case complexity of binary search algorithm? Make I mistakes?

No Name
  • 13
  • 1
  • 3
  • Welcome to [cs.SE]! Let me direct you towards our reference questions which cover techniques that can be applied to solve your problem, in particular http://cs.stackexchange.com/q/2789/755. – D.W. Dec 14 '16 at 09:52

2 Answers2

2

That's a way to do it. Sometimes it's easier to go the other way round: What is the size of the largest array where binary search will locate an item or determine it's not there, using k comparisons? And it turns out that the largest array has size $2^k - 1$. And then you reverse this.

Understanding the master method is very useful if you have to pass a test that tests whether you understand the master method. But be prepared to have more practical problem where the only method that works is the "turn on your brain" method. A simple example is the Greatest Common Divisor algorithm.

gnasher729
  • 29,996
  • 34
  • 54
1

A much better way is to use the master method :), check that out!

Logan Leland
  • 160
  • 6
  • Here is a useful document when working on things like this: it includes the master method. https://www.google.com/url?sa=t&source=web&rct=j&url=http://www.tug.org/texshowcase/cheat.pdf&ved=0ahUKEwiz4dWRwPLQAhUlIcAKHdTlBbUQFggaMAA&usg=AFQjCNFeZx9JevUGn2nkvYmv0N5J9mr8XQ&sig2=CetJqZOlZO0SB7N6go08KA – Logan Leland Dec 14 '16 at 01:11
  • Thx. I will check this document. BTW. My solution is good? – No Name Dec 14 '16 at 01:26
  • This solution is okay – Logan Leland Dec 14 '16 at 01:27