Daily algorithm practice: Runtime analysis

From Cracking the Coding Interview: 

I had to peek at the solution for this one, but it makes sense now. This may not be the best explanation but I'm sure I'll get better as I go along. Also, when I get to the true exercises (instead of just the examples) I will probably put them on my GitHub as well. But not for the examples. 

In this algorithm, we will be repeatedly dividing by 2. We will keep recursing until we can't divide by 2 anymore. And when we have an algorithm in which we keep diving by 2, this is a pretty clear indicator of O(log n) runtime. Why? Because if we kept multiplying by 2 repeatedly, that would be an exponential runtime. Logarithms are the opposite of powers, we are diving here, and multiplication is the opposite of division. Ergo, logarithmic runtime. 

Comments

Popular posts from this blog

6 hours and only 6 Lagoon rides?

Reflecting on major life goals yet again

Hobble Creek Half: The slow passage of time marches us towards our inevitable death