...
/Example: Measuring Time Complexity of a Single Loop Algorithm
Example: Measuring Time Complexity of a Single Loop Algorithm
In this lesson, we are going to learn how to compute the time complexity of an algorithm that involves a for loop.
We'll cover the following...
In the previous lesson, we calculated the time complexity of the algorithm implemented in a simple C++ program.
A For Loop With n
Iterations #
Now, let’s consider what happens when a loop is involved. Consider the following C++ program::
Press + to interact
#include <iostream>using namespace std;int main(){int n = 10;int sum = 0;for (int i = 0; i < n; i++)sum += 2;cout << sum;return 0;}
Let’s count the number of primitive operations in the above program. We skip the non-executable lines and come to lines 4 and 5 where variable initializations are taking place, which account for one primitive operation, each.
Line 6 is a loop statement. To count the number ...
Access this course and 1400+ top-rated courses and projects.