- Measures how an algorithm slows down as input grows
- Only the worst case matters
- Constants get dropped, so \(100n\) is just \(O(n)\)
- Helps compare algorithms before writing any code
Ordered from best to worst:
\[O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n)\]
Check each element one by one until the target is found.
\[T(n) = n\]
Works on any list. Gets slow on large ones.
Requires a sorted list. Cuts the search space in half each step.
\[T(n) = \log_2 n\]
On 1,000,000 elements: binary search takes ~20 steps, linear takes up to 1,000,000.
n = 1:1000
searchDf = data.frame(
n = rep(n, 2),
steps = c(n, log2(n)),
type = rep(c("linear search", "binary search"), each=length(n))
)
ggplot(searchDf, aes(x=n, y=steps, color=type)) +
geom_line(linewidth=1) +
labs(x="input size (n)", y="steps", color="algorithm") +
theme_minimal()
Two nested loops over different sized inputs. Total operations are n times m.