Skip to content

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.

Beginner 10 min read·Lesson 267 of 306 in Python Programming

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

Python
items[i] # O(1)
key in my_dict # O(1) average
x in my_list # O(n)
sorted(items) # O(n log n)
for a in xs: # O(n²)
for b in xs: ...
  • Keep the dominant term.
  • Drop constants.
  • Sequential adds, nested multiplies.

Code examples

How the classes grow

Python
import math
for n in [10, 100, 1000]:
print(f"n={n:<5} log2={math.log2(n):5.1f} nlogn={n * math.log2(n):8.0f} n^2={n * n:>8}")
Output
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

Python
def first(xs): # O(1)
return xs[0]
def total(xs): # O(n)
return sum(xs)
def pairs(xs): # O(n^2)
return [(a, b) for a in xs for b in xs if a < b]
xs = [3, 1, 2]
print(first(xs), total(xs), pairs(xs))
Output
3 6 [(1, 3), (1, 2), (2, 3)]

More examples

What doubling n does

Python
def ops_log(n):
steps = 0
while n > 1:
n //= 2
steps += 1
return steps
def ops_linear(n):
return n
def ops_quadratic(n):
return n * n
for name, f in [("O(log n)", ops_log), ("O(n)", ops_linear), ("O(n^2)", ops_quadratic)]:
print(f"{name:<9}{f(1000):>9}{f(2000):>9} x{f(2000) / f(1000):.1f}")
Output
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:

TextOutput
O(log n) 9 10 x1.1
O(n) 1000 2000 x2.0
O(n^2) 1000000 4000000 x4.0

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

O(3n + 10)

Prefer

O(n)

Big-O describes growth; constants and smaller terms are dropped.

Assuming built-ins are free

Avoid

while x in big_list: big_list.remove(x) # 'one line, so fast'

Prefer

big_list = [y for y in big_list if y != x]

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

  1. 1
    Give the Big-O of five functions you've written in earlier lessons.
  2. 2
    Explain 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).
Practise this topic