(Almost) Daily Algorithm Practice: Big O and square roots

Here is today's problem. I was definitely off-base on my first attempt at analyzing the runtime! 



But when you realize what this is doing, it's not too bad. While of course in a real programming scenario we'd just use one of the built-in square root functions, well...Someone has to code those to work in the first place! And this is a possible implementation of one. 

If you notice, the algorithm is essentially dividing the problem space into two pieces and analyzing them, effectively running a binary search. This was my initial mistake. I stepped through the problem, was doing all this division crap, forgetting that it's not about the operations that the algorithm itself is running, but rather the approximate number of times it is running said operations. 

So, it's a binary search, basically to the T. There is nothing special here. Consequently, we can conclude that the runtime is O(log n)

Comments

Popular posts from this blog

Unleashing my inner Disney Princess ✩₊˚.⋆☾⋆⁺₊✧ at the 2024 Disney Princesses Half Marathon

The 20-something types of Computer Science majors

The Evenstar