Search⌘ K
AI Features

Example: Measuring Time Complexity - Nested Loop

Compute time complexity of a given algorithm with nested loops

We'll cover the following...

A nested loop

We will extend the previous example and make it a little harder by adding a “nested loop” in the program. We will calculate its time complexity by ...

Python 3.5
n = 5 # n can be anything
sum = 0
for i in range(n):
for j in range(n):
sum += 1
print(sum)

Time complexity

We will first break this program into individual operations like this:

Statement Number of Executions
n = 5 1
sum = 0 1
range(n) line 3
...