I just did a complexity analysis, I would love to hear some feedback if my thought process is ok:
I learn phyton by the way
the code:
def f(L):
n = len(L)
while n > 0:
n = n // 2
for i in range(n):
if i in L:
L.append(i)
return L
my analysis:
- the while loop is an O(log n) complexity ( easy to see, since its n/2 then n/2^-2..... n/2^-n
- the inner part is the more complex part for me the loop itself is actually the sum of geometric series, where the first element is n,with q= 1/2, and N = log n $$ S_{n:}=n\cdot \sum _{i=1}^{log:n}\left(\frac{1}{2}\right)^i=\frac{n\left(\left(2^{-1}\right)^{log:n}-1\right)}{\frac{1}{2}-1}=\frac{1}{-\frac{1}{2}}+\frac{n}{\frac{1}{2}}=n $$ image of the equation, since latex seem to do problem
and the append is another function with O(n)
so combining everything it should be O(n^2 log n ) the solution that appear in my book say it should be O(n^2), but there is no further explanation I guess that there is something wrong with my inner loop analysis.
thanks to all