Which Algorithm Is Better?
The fastest-possible running time for any runtime analysis is O(1), commonly referred to as constant
running time. An algorithm with constant running time always takes the same amount of time
to execute, regardless of the input size. This is the ideal run time for an algorithm, but it’s rarely
achievable.
The performance of most algorithms depends on n, the size of the input. The algorithms can be classified
as follows from best-to-worse performance:
➤➤ O(log n) — An algorithm is said to be logarithmic if its running time increases logarithmically
in proportion to the input size.
➤➤ O(n) — A linear algorithm’s running time increases in direct proportion to the input size.
➤➤ O(n log n) — A superlinear algorithm is midway between a linear algorithm and a polynomial
algorithm.
➤➤ O(nc) — A polynomial algorithm grows quickly based on the size of the input.
➤➤ O(cn) — An exponential algorithm grows even faster than a polynomial algorithm.
➤➤ O(n!) — A factorial algorithm grows the fastest and becomes quickly unusable for even
small values of n.