Algorithm Analysis
Common mistakes
- Comparing algorithms by timing one run instead of by growth rate.
- Reporting only the best case when the worst case is what users hit.
Interview questions
What are best, average and worst case in algorithm analysis?
They are the cost on the most favorable input, a typical input and the least favorable input. Linear search is O(1) in the best case and O(n) in the worst case.
Is an algorithm with lower Big O always faster in practice?
No. For small inputs, constant factors and memory access can dominate. That is why many sort implementations switch to insertion sort for short runs.
Should I benchmark code or analyze its complexity?
Both. Analysis tells you how cost grows with input size; benchmarks on realistic data tell you the actual speed.