All,
I have always found myself being doubtful when it comes to finding the complexity of a given code/algorithm. Ex.
FOR I=1 TO N
do J=1
WHILE J*J < I
do J=J+1
The above code has time complexity of Big Theta (N^(3/2)) . However, I don’t understand how the answer is derived.
Can anyone please guide me to the steps for finding the complexity or any specific resource that can help me? Most of the times, I only find code with complexity N, lg N , N lg N and N^2
The approach is always the same: work out how many operations are being executed as a function of N, and then throw away low-order terms and constants.
So in your example above, the inner loop iterates approximately sqrt(I) times, so we have (approximately) sqrt(1) + sqrt(2) + sqrt(3) + … The result is a function whose highest-order term is N^(3/2).