Example: Measuring Time Complexity of For Loop
Explore how to measure the time complexity of a simple for loop by breaking down each operation, counting executions, and summing them to determine overall running time. This lesson helps you understand the principles behind evaluating algorithm performance step-by-step.
We'll cover the following...
We'll cover the following...
Lets now calculate the running time complexity of a more complex program. We will split the code into individual operations and then compute how many times each is executed.
Simple For Loop of Size n #
Here is an example of a simple loop of size n:
| Operation | Number of executions |
|---|---|
n = 10 |
1 |
sum = 0 |
1 |
range(n) |
|
var=0 |
1 |
var=1 |
1 |
var=2 |
1 |
| … | |
var=n-1 |
1 |