7 Comparing Algorithms
7 Comparing Algorithms
In this lesson, we are going to learn how two or more algorithms may be
compared.
• Introduction(https://www.educative.io/courses/algorithms-coding-
interviews-cpp/m2M5016E4mG#introduction)
• Important (https://www.educative.io/courses/algorithms-coding-
Criteria: Time interviews-cpp/m2M5016E4mG#important-criteria-time-
and Space and-space)
• Comparing (https://www.educative.io/courses/algorithms-coding-
Execution interviews-cpp/m2M5016E4mG#comparing-execution-time)
Time
• Experimental(https://www.educative.io/courses/algorithms-coding-
Evaluation interviews-cpp/m2M5016E4mG#experimental-evaluation)
• Analytical (https://www.educative.io/courses/algorithms-coding-
Evaluation interviews-cpp/m2M5016E4mG#analytical-evaluation)
• Best Case (https://www.educative.io/courses/algorithms-coding-
Analysis interviews-cpp/m2M5016E4mG#best-case-analysis)
• Worst- (https://www.educative.io/courses/algorithms-coding-
Case interviews-cpp/m2M5016E4mG#worst-case-analysis)
Analysis
• Average (https://www.educative.io/courses/algorithms-coding-
Case interviews-cpp/m2M5016E4mG#average-case-analysis)
Analysis
• The (https://www.educative.io/courses/algorithms-coding-
Verdict interviews-cpp/m2M5016E4mG#the-verdict)
• Analyzing a (https://www.educative.io/courses/algorithms-coding-
Simple C++ interviews-cpp/m2M5016E4mG#analyzing-a-simple-c-
Program program)
Note: If you have already covered our course Data Structures in C++: An
Interview Refresher
(https://www.educative.io/collection/5642554087309312/564627607912
4480?authorName=Coderust) you may skip this chapter, since a chapter
on Algorithmic Complexity Measures was a part of the said course, too.
Introduction #
(https://www.educative.io/courses/algorithms-
coding-interviews-
cpp/m2M5016E4mG#introduction)
There are typically several different algorithms to solve a given computational
problem. It is natural, then, to compare these alternatives. But how do we know
if algorithm A is better than algorithm B?
Experimental Evaluation #
(https://www.educative.io/courses/algorithms-coding-
interviews-cpp/m2M5016E4mG#experimental-evaluation)
Well, we could implement the two algorithms and run them on a computer
while measuring the execution time. The algorithm with less execution time
wins. One thing is for sure; this comparison must be made in a fair manner.
Let’s try to punch holes into this idea:
The number of times conditional statements run depends on the actual input
values. Sometimes a code block is executed, some times it isn’t. So, how do we
account for conditional statements? We can adopt one of three strategies: best
case analysis, average-case analysis, and worst-case analysis.
In the best case analysis, we consider the specific input that results in the
execution of the fewest possible primitive operations. This gives us a lower
bound on the execution time of that algorithm for a given input size.
Worst-Case Analysis # (https://www.educative.io/courses/algorithms-coding-
interviews-cpp/m2M5016E4mG#worst-case-analysis)
In the worst-case analysis, we consider the specific input that results in the
execution of the maximum possible primitive operations. This gives us an upper
bound on the execution time of that algorithm for a given input size.
The best-case analysis has limited value because what if you deploy that
algorithm and the best case input rarely occurs. We feel that the worst-case
analysis is more useful because whatever answer it gives you, you can be sure
that no matter what, algorithm A wouldn’t incur more time than that. Unless
otherwise specified, our analysis in this course will be worst-case running time.
#include <iostream>
using namespace std;
int main() {
int x = 0;
x += 1;
cout << x;
return x;
}
Lines 1 and 2 are compiler directives, so we just ignore them. There is a variable
assignment on line number 5, so that’s 1 primitive operation. A variable’s value
is accessed, an addition is performed, and then an assignment takes place on
line 6, so that’s three primitive operations. A variable’s value is accessed and
then displayed on line 7, so that’s two primitive operations. A variable’s value is
accessed and returned on line number 8, so that’s two primitive operations. So,
overall there are 8 primitive operations in the above program. This analysis is
also presented in the following table:
5 Variable assignment 1
Variable access,
6 addition, variable 3
assignment
Primitive No. of Primitive
Line No.
operations Operations
Variable access,
7 2
display value
For the above program, the time complexity can be specified as:
Time Complexity = 1 + 3 + 2 + 2 ⇒ 8
Note that there is no notion of input size in this simple example since there is no
input to this program. Algorithms for which the time complexity is independent
of the input size are known as constant-time algorithms.
In the next lesson, we will analyze the running time of an algorithm involving a