Time complexity
Lesson 7 of 26 in Coddy's Arrays in C++ course.
Running a program takes time. and knowing beforehand how much time it takes is useful for making efficient programs. In programming, the amount of time required to run a program is called time complexity.
As an example
for(int i=0;i<5;i++{
cout<<i<<endl;
}In the example above the loop iterates five times therefore the time complexity is 5. In short, time complexity refers to the number of iterations the program is doing. If a program iterates one time then the complexity is one, but if it iterates some arbitrary number n then the time complexity is n
Time complexity uses the big O notation to illustrate the complexity, and we never write constant values inside the notation. For example, five iterations or six iterations are both referred to as O(1), whereas N iterations or 2N iterations are referred to as O(N). When analyzing the time complexity of a program, we take the worst case and calculate how many iterations it can run.
For this particular course, we need to understand the importance of time complexity and some types of it. We will discuss it in the next lesson.
Go through the below article to know more about time complexity analysis.
Try it yourself
This lesson doesn't include a code challenge.