Time Complexity Zoo [00L1]
- September 10, 2026
We now present a “complexity zoo”, with examples of things having each common asymptotic time complexity, with some relevant facts interspersed. Our zoo will be ordered from smallest to biggest; that is, each entry will be little-o of the next thing. For each \Theta (f(n)), we will also record the largest n such that we can process an input of size n using a \Theta (f(n))-time algorithm in approximately 1 second, assuming we can do 10^8 operations per second.
\Theta (1): constant (any input) [00L2]
- September 10, 2026
A \Theta (1) or constant time algorithm is one whose running time does not depend on the size of its input. If we have an algorithm that takes \Theta (1), we can obviously process arbitrary-sized inputs in less than one second.
Examples of \Theta (1) operations include:
- Access an arbitrary index in an array
- Push to or pop from the top of a stack
- Add to or remove from the end of an extensible list (e.g.
ArrayListin Java; Python lists) - Insert, update, or remove from a hash table (e.g.
set+ dictionaries in Python;HashSet/HashMapin Java) - Add to or remove from either end of a deque (double-ended queue)
- Arithmetic (usually! sort of!)
\Theta (\lg n): logarithmic (unimaginably large inputs) [00L3]
- September 10, 2026
We say something is logarithmic if it is proportional to \log n for inputs of size n.
Notation \lg [00L4]
- September 10, 2026
We will use \lg n as an abbreviation for \log _2 n. Two is by far the most common and useful base for logarithms when talking about computers.
If we have an algorithm that takes \Theta (\lg n) time, we could use it to process inputs of size 2^{10^8} in one second, which is unimaginably large.
Examples of things that are \Theta (\lg n):
- Binary search
- The height of a balanced binary tree with n nodes
- The number of bits needed to represent n
Theorem Logarithm base doesn't matter to \Theta [00L9]
- September 11, 2026
\Theta (\log _a n) = \Theta (\log _b n) for all positive real numbers a and b. In other words, up to \Theta , the base of a logarithm doesn't matter; \Theta (\log n), \Theta (\lg n), \Theta (\ln n), etc. are all the same.
Proof.
- September 11, 2026
Theorem Logarithms grow more slowly than any power [00L8]
- September 11, 2026
\log _b n is o(n^x) for all x > 0 and any b > 1.
Proof.
- September 11, 2026
Remark \lg n is much closer to 1 than n [00LA]
- September 11, 2026
This is somewhat surprising! \Theta (\lg n) is actually smaller than n to any positive power: even \Theta (\sqrt [15]{n}) or whatever. \Theta (\lg n) is not “halfway between” \Theta (1) and \Theta (n); in some sense it is much closer to \Theta (1).
For example, the biggest number that fits in a 64-bit integer is about 10^{19}. How long would it take to count that high? At 10^8 per second, it would take 10^{11} seconds or about 3000 years. But if we can process it using something that repeatedly halves, \lg 10^{19} \approx 63.
\Theta (\sqrt n) = \Theta (n^{1/2}) (inputs up to 10^{16}) [00LB]
- September 11, 2026
This will not come up much in this course, but it is definitely a thing. See “square root decomposition”.
\Theta (n): linear (inputs up to 100 million) [00LC]
- September 11, 2026
Examples of things that are \Theta (n):
- Linear search
- Maximum/minimum/sum of a list
- Merge two sorted lists
- Insert or removing at the beginning of an array/list.
\Theta (n \lg n) (inputs up to 4.5 million) [00LD]
- September 11, 2026
Examples of things that are \Theta (n \lg n):
- Mergesort, quicksort
- In general, divide & conquer algorithms with \Theta (n) work at each level. We’ll study this in more detail later in the semester.
- If you are only allowed to compare pairs of elements to see which is greater, sorting a list of n elements takes \Omega (n \lg n). In other words, it's impossible to sort any faster than n \lg n if you are only allowed to compare elements. (If you are allowed extra operations, sorting faster than n \lg n is possible; for example, a list of integers can be sorted in \Theta (n) time.)
\Theta (n^2): quadratic (inputs up to 10 thousand) [00LE]
- September 11, 2026
Examples of things that are \Theta (n^2):
- Two nested loops (often, but not necessarily)
- 1 + 2 + 3 + \dots + n = \frac {n(n+1)}{2} = \Theta (n^2)
- The number of pairs of items chosen out of n, \binom {n}{2} = \frac {n(n-1)}{2}.
- Repeatedly appending n characters to a string one by one
- Insertion sort, bubble sort
\Theta (n^k) (inputs up to \sqrt [k]{10^8}) [00LF]
- September 11, 2026
\Theta (2^n): exponential (up to n = 26) [00LH]
- September 11, 2026
Examples of things that are \Theta (2^n):
- Number of subsets of n things
- Number of bitstrings of length n
- Number of nodes (also number of leaves) in a tree of height n
- 1 + 2 + 4 + 8 + \dots + 2^n = 2^{n+1} - 1, so it is \Theta (2^n)
Theorem [00LI]
- September 11, 2026
n^k is o(r^n) for all integers k \geq 0 and real numbers r > 1.
Proof.
- September 11, 2026
Remark Polynomial vs exponential [00LJ]
- September 11, 2026
There is an insurmountable gulf between polynomial and exponential time. For example, even n^{295} is o(1.001^n)! We usually take this to be the dividing line between “efficient/feasible” and “inefficient/infeasible”. (Of course n^{295} is probably not actually feasible but in practice we don’t see algorithms with running times like that.)
\Theta (n!): factorial (inputs up to 11) [00LK]
- September 11, 2026
n! corresponds to the number of distinct orderings of n things. If an algorithm takes \Theta (n!) it is really, really bad. In fact, k^n is o(n!) for all k.