I am trying to figure out the time complexity (Big-O) of functions and try to provide an appropriate reason.
First function:
r = 0 # Assignment is constant time. Executed once. O(1) for i in range(n): for j in range(i+1,n): for k in range(i,j): r += 1 # Assignment and access are O(1). Executed n^3
like this.
I see that this is a triple nested loop, so it should be O (n ^ 3). but I think that my reasoning here is very weak. I really donβt understand what is going on inside inside a triple nested loop here
Second function:
i = n
I found that this algorithm is O (1). But, like the first function, I do not see what happens in the while loop.
Can someone explain in detail about the time complexity of the two functions? Thanks!
source share