This function is O(log(n)). Why? Isn't it looping up to n?
function fxn($n) {
for ($i = 1; $i <= $n; $i *= 2)
echo $i;
}
I'm pretty new to O(n) analysis by the way. This function sure looks O(n) though since it loops up to n.
This function is O(log(n)). Why? Isn't it looping up to n?
function fxn($n) {
for ($i = 1; $i <= $n; $i *= 2)
echo $i;
}
I'm pretty new to O(n) analysis by the way. This function sure looks O(n) though since it loops up to n.
Note: that your function will never end because you're starting at 0, and 0 * 2 = 0. I'll assume your loop starts at 1.
The loop increments by a multiple of 2 every time, which is why the runtime is
O(lg(n))
.Let's consider a simple example where n = 128.
here are the values of i for each iteration: 1, 2, 4, 8, 16, 32, 64, 128. Thus, you've gone through 8 values.
Note here that the
- 1
is a constant, so it doesn't affect our runtime calculation.If the loop incremented by 1 (or any constant, k) the runtime would be O(n). The important thing to consider here is the difference between a geometric series and an arithmetic series, which gives you different runtimes.
Your code loops up to
n
but not by ones (or any constant value) which would make it O(n).This is what it does:
Because it's doubling each time, it's actually O(log N), similar to the way a binary tree search halves the search space on each iteration.
This loop would be O(n):
Because
$i
takes on the values:But this loop is only O(log(n)):
Because
$i
takes on the values:And a sequence that grows in that manner is called "logarithmic".
It's not looping through all of the elements, in each step it jumps over twice of the elements of the previous step - because of the
$i *= 2
part. That is, assuming that$i
starts in a value greater than zero, otherwise it's an infinite loop:$i
will always be0
as it is written.