Time Complexity of Algorithms
Learn about fast vs. slow algorithms and the big-O notation to estimate the time complexity of algorithms.
We'll cover the following
Fast vs. slow algorithms
Real computers require a certain amount of time to perform an operation such as addition, subtraction, or testing the conditions in a while loop. A supercomputer might take seconds to perform addition, while a calculator might take seconds. Suppose we had a computer that took seconds to perform an elementary operation such as addition, and we knew how many operations a particular algorithm would perform.
We could estimate the running time of the algorithm simply by taking the product of the number of operations and the time per operation. However, computers are constantly improving, leading to a decreasing time per operation, so our notion of the running time would soon be outdated.
Rather than computing an algorithm’s running time on every computer, we rely on the total number of operations that the algorithm performs to describe its running time since this is an attribute of the algorithm and not an attribute of the computer we happen to be using.
Unfortunately, determining how many operations an algorithm will perform is not always easy. Suppose we know how to compute the number of basic operations that an algorithm performs. In that case, we have a basis for comparing it against a different algorithm that solves the same problem. Rather than tediously counting every multiplication and addition, we can perform this comparison by gaining a high-level understanding of the growth of each algorithm’s operation count as the size of the input increases.
Suppose an algorithm performs operations on an input of size , and an algorithm solves the same problem in operations. Which algorithm, or , is faster? Although may be faster than for some small (for example, for between and ), will become faster for large (for example, for all ). See the figure below. Since is, in some sense, a faster-growing function than with respect to , the constants and in do not affect the competition between the two algorithms for large . We refer to as a quadratic algorithm and to as a linear algorithm and say that is less efficient than because it performs more operations to solve the same problem when is large. Therefore, we will often be somewhat imprecise when we count the operations of an algorithm—the behavior of algorithms on small inputs does not matter.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.