Comparing Algorithms

In this lesson, you will learn how to compare two or more algorithms.

Introduction

There are typically several different algorithms used to solve a given computational problem. It is natural, then, to compare these alternatives. But how do you know if algorithm A is better than algorithm B?

Important Criteria: Time and space

One important factor that determines the “effectiveness” of an algorithm is the amount of time that the algorithm will take to solve a given problem. If algorithm A takes less time to solve the same problem than algorithm B, then algorithm A is considered better.

Another important factor in comparing two algorithms is the amount of memory required to solve a given problem. The algorithm that requires less memory is considered better.

Comparing execution time

The remainder of this lesson will focus on the first factor, i.e., execution time. How do you compare the execution time of two algorithms?

Experimental evaluation

Well, you could implement the two algorithms and run them on a computer while measuring the execution time. The algorithm with less execution time wins. But one thing is certain: this comparison must be made in a fair manner. Let’s try to understand this better:

  • An algorithm might take longer to run on an input of greater size. Thus, algorithms that are being compared must be tested on the same input size. But that’s not all. Due to the presence of conditional statements, for a given input size, even the same algorithm’s running time may vary with the actual input given to it. This means that the algorithms that are being compared must be tested on the same input. Since one algorithm may be disadvantaged over another for a specific input, you must test the algorithms exhaustively on all possible input values. But this is not possible.
  • The algorithms that are being compared must first be implemented. What if the programmer comes up with a better implementation of one algorithm than the other? What if the compiler
...