...
/Example 2: Time Complexity of an Algorithm With Nested Loops
Example 2: Time Complexity of an Algorithm With Nested Loops
This example is about computing the time complexity of an algorithm that involves nested for-loops.
We'll cover the following...
In the previous lesson, you learned how to calculate the time complexity of an algorithm that involves a loop. Now, you will extend the same idea to analyzing an algorithm with nested for-loops.
Nested for
loop
Consider the following C# program:
Press + to interact
namespace Chapter_1{class Example2{static void Main(string[] args){int n = 5;int m = 7;int sum = 0;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++)sum += 1;}Console.Write(sum);return;}}}
A simple piece of code prints the number of times the increment statement runs throughout the program. Compute its time complexity.
Time complexity
Take the training wheels off, and jump straight to line 10. As seen from the previous lesson, line 10 accounts for ...