Daily algorithm practice: Runtime analysis
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
Post a Comment