Asymptotic Notation
This chapter introduces Big-Oh notation, a way to measure how much work an algorithm does that does not depend on the machine it runs on. It covers the two rules that turn a step count into a Big-Oh, how to read a Big-Oh directly from code, and how to use it to compare DynamicArray and SortedArray and choose between them for a workload.
After reading this chapter, you should be able to:
- Explain why timing a run or counting one chosen operation both fail to measure how an algorithm scales, and why we want a measure of work that ignores the machine, language, and coding style
- Translate a cost expression such as into Big-Oh by suppressing coefficients and dropping lower-order terms, and justify why both simplifications are valid
- Read an algorithm’s Big-Oh directly from its code by locating the dominant work, including work built from calls into other operations: sequential loops add, nested loops multiply, a triangular loop is still , and a loop counter that multiplies or divides by a constant each iteration is
- Reason about
DynamicArrayandSortedArrayoperation costs from what each structure must do, and choose between them for a workload by weighing search frequency against update frequency, and explain why sorting on demand does not avoid that trade-off - Interpret a Big-Oh class as a family of functions sharing a growth rate, order the common families from through , and determine which of two algorithms is faster for large
Sections
- Why We Need a Way to Analyze Algorithms
- Why Not Just Time Them?
- Big-Oh Notation
- Why We Suppress Coefficients
- Why We Drop Lower Terms
- Reading Big-Oh from Code
- Two Loops, Still Linear
- Nested Loops Multiply
- Half the Work, Same Class
- A Loop That Doubles
- Building Up to Selection Sort
- Reasoning About Operations Without the Code
- Which Structure for the Roster?
- Why Not Sort Only When We Search?
- Growth Rate
- Common Running Times