Module 18 · Algorithms and Problem Solving — topic 7 of 18
Big-O notation
The standard language for algorithm efficiency: what O(…) means, dropping constants and smaller terms, the common classes O(1), O(log n), O(n), O(n log n), O(n²) and O(2ⁿ), and the complexity of everyday Python operations.
Learning objectives
- Read and write Big-O expressions
- Simplify complexity by dropping constants and smaller terms
- Know the complexity of common Python operations
Definition
Big-O notation describes an upper bound on how an algorithm's cost grows as the input size n becomes large. O(n²) means the work grows at most proportionally to n², ignoring constant factors and lower-order terms.
Introduction
'This is O(n log n)' is a compact way of saying 'this scales well, like a good sort'. Big-O lets programmers compare algorithms in a few characters.
Why it matters
Big-O is used in documentation, code reviews and interviews. It tells you how code will behave on tomorrow's larger data.
Detailed explanation
Big-O keeps only the dominant term and drops constants: 3n² + 5n + 2 becomes O(n²); 2n becomes O(n).
O(1) constant: indexing a list, dictionary and set lookups (on average), appending to a list.
O(log n) logarithmic: binary search, operations on balanced trees and heaps (heapq push/pop).
O(n) linear: a single loop, sum, max, x in list, list.index, copying a list.
O(n log n): efficient sorting such as Python's sorted (Timsort), merge sort.
O(n²) quadratic: nested loops over the same data, bubble/selection/insertion sort. O(2ⁿ) exponential: trying every subset, naive recursive Fibonacci.
Sequential steps add (O(n) + O(n) = O(n)); nested steps multiply (O(n) inside O(n) = O(n²)).
Big-O describes growth, not exact speed: for small inputs a 'worse' algorithm can be faster, but for large inputs growth wins.
Syntax
- Keep the dominant term.
- Drop constants.
- Sequential adds, nested multiplies.
Code examples
How the classes grow
n=10 log2= 3.3 nlogn= 33 n^2= 100 n=100 log2= 6.6 nlogn= 664 n^2= 10000 n=1000 log2= 10.0 nlogn= 9966 n^2= 1000000
Recognising complexity
3 6 [(1, 3), (1, 2), (2, 3)]
More examples
What doubling n does
O(log n) 9 10 x1.1 O(n) 1000 2000 x2.0 O(n^2) 1000000 4000000 x4.0
Step-by-step explanation
- Doubling the input adds just one step for a logarithmic algorithm.
- A linear algorithm does twice the work.
- A quadratic algorithm does four times the work — and 100 times the work if n grows tenfold.
Expected output
Running “What doubling n does” prints exactly:
Practical applications
- Library documentation listing operation complexities.
- Choosing data structures in code reviews.
Best practices
- Know the costs of list, dict and set operations.
- Aim for O(n) or O(n log n) on large data.
- Measure when constants might matter.
Common mistakes
Keeping constants
Avoid
Prefer
Big-O describes growth; constants and smaller terms are dropped.
Assuming built-ins are free
Avoid
Prefer
in and remove are each O(n), making the loop O(n²).
Important notes
- Big-Θ (theta) and Big-Ω (omega) describe tight and lower bounds; Big-O is the one used most often in practice.
Key takeaways
- Big-O gives the growth rate of cost.
- Common classes: 1, log n, n, n log n, n², 2ⁿ.
- Drop constants; add sequential steps, multiply nested ones.
Try it yourself
- 1Give the Big-O of five functions you've written in earlier lessons.
- 2Explain why checking membership in a set is faster than in a list for large data.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
2 questions test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedWhat is the average time complexity of x in my_set?
- AdvancedSimplify O(3n² + 10n + 7).