Common Running Times
Big-oh puts the amount of work an algorithm does, as the input size grows, into a family of functions that share a growth rate.
The amount of work an algorithm does affects how long it takes to run. We often use the terms running time and runtime to mean the amount of work an algorithm does, and we use faster and slower to compare the running times of two algorithms. The faster one does less work for large inputs, and the slower one does more.
Here are the common running time families, ordered from the slowest-growing at the top (faster running times) to the fastest-growing at the bottom (slower running times):
- , constant. The work does not depend on the input size at all. Reading an element by index, or
swap, or appending to aDynamicArray. Remember that does not mean one step, only a number of steps that stays fixed as grows. - , logarithmic. The work grows, but much more slowly than the input, because each step eliminates a constant fraction of what is left. Binary search is , and we will see other logarithmic algorithms later.
- , linear. The work is proportional to the input. Linear search, or any single pass over an array, like
indexOfMin. - , linearithmic. A little more than linear. The fastest general-purpose sorting algorithms are in this family. We will see them later.
- , quadratic. The work is proportional to the square of the input. Selection sort is in this family. We will see other quadratic sorting algorithms later.
- , polynomial. Any constant power ; quadratic is just the case . The cost grows quickly as grows.
- , exponential. A constant raised to the input, so the work multiplies by roughly for each element added to the input. Trying every subset of items is .
- , factorial. Trying every ordering of items. This is too slow for anything but very small .
These few families are enough to describe nearly every algorithm in this course, and most of the ones you would see in a coding interview. Given two algorithms, the one in the slower-growing family is the faster one for large .
As a rough guide:
-
Practically efficient (still usable on large inputs): , , ,
-
Practically inefficient (too slow on large inputs): , ,