Question

I have written the running time based on my calculation in red color on the picture. But I don't know what would be for that IF statement as well as it's body's running time. Will that IF condition's running time would be n x n ? Because each time in the inner loop, it would be evaluated. Isn't it? What about it's body's?

enter image description here

Was it helpful?

Solution

You need to take into account two different parameters, m and n. The outermost loop executes m times, and the inner loop executes n times. The complexity, therefore, is θ(mn).

Note that the if-statement and its body are executed in constant time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top