I am not sure at all if I understand calculations of the time complexity. I am given these loops, and these are my calculations. However, I am not sure at all what to say about the big O's here.
Loop 1:
lst = []
i=1
while i<n:
lst = list(range(i))
i *= 2
I assume that each operation takes O(1) time. In this loop, the 1st line and the second line are executed 1 time each. The first line of the while loop has 3 operations - range, list, and assignment of that value to lst. Since we are dealing with range, I assume that it runs n+1 times.
The last line of the loop has 2 operations: multiplication by 2 and assignment of that value to i, and it runs n times.
From this, I think that the total would be: 1+1+3(n+1)+2n = 5n+5.
Since this is a linear function, then the big O will be O(n)?
===============
Loop 2:
lst = []
i=1
while i<n:
lst = lst + [i]
i *= 2
Here we have a similar case, however the first line of the while loop has 2 operations.
Then,
1+1+2n+2n = 4n+2.
Since it's a linear function, it's O(n) as well?
========================== Loop 3:
lst = []
i=1
while i<n:
lst += [i]
i *= 2
I think that lst +=[i] would have 2 operations executed 2n times, since this is an in-place calculation? I am not sure about this. If this is right, then the total would be 6n+2
The questions are: am I calculating these right or am I completely wrong? How do I write the big O's for each of these loops?