HomeAll tracks · pricing · account
Python11 apps · Python, SQL & data
Start free Log in
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.
Subscribe to unlock this lesson.

📏 Rules of thumb0 of 80 lessons passed

The short version of every algorithm

Each lesson ends on a poster: its cost at a glance, when to use it, when not to, a trap or a tip, and one line to remember. Pass a lesson and its poster appears here.

⏱️ How big can n be?

Python runs very roughly 10 million simple steps a second (compiled languages about 100 times more). This is how large the input can be for an answer in about one second.

CostNameLargest n in ~1 secondTypical example
O(1)constantany nLook up a list item by index, a dict key
O(log n)logarithmicany nBinary search
O(n)linear10 millionOne pass over the data
O(n log n)linearithmicabout 500,000Merge sort, `sorted()`
O(n²)quadraticabout 3,000Every pair: two nested loops
O(n³)cubicabout 200Three nested loops
O(2ⁿ)exponentialabout 23Every subset
O(n!)factorialabout 10Every ordering

Thinking in steps

Largest value

Rules of thumb
BestO(n)
AverageO(n)
WorstO(n)
Extra spaceO(1)
one passn − 1 comparisons
  • Use it whenYou need one summary of a list (largest, smallest, total): one pass with a running variable.
  • TipSay what your variable means after every step. If that stays true, the answer is right.
  • TipIn real code call `max(values)`: the same algorithm, already written and tested.
  • Watch outStarting with `best = 0` breaks on all-negative lists. Start from the first value.
  • Avoid it whenSorting just to take the last value: a sort does far more work than one pass.

RememberOne pass, one running best: n values, n − 1 comparisons.

02 · One loop: counting stepsPass this lesson to add its rules of thumb here.Go to the lesson →
03 · Nested loops: O(n²)Pass this lesson to add its rules of thumb here.Go to the lesson →
04 · Halving: O(log n)Pass this lesson to add its rules of thumb here.Go to the lesson →
05 · Big-O, Big-Ω and Big-ΘPass this lesson to add its rules of thumb here.Go to the lesson →
06 · Comparing growthPass this lesson to add its rules of thumb here.Go to the lesson →

Searching

07 · Linear search: best, average and worst casePass this lesson to add its rules of thumb here.Go to the lesson →
08 · Binary searchPass this lesson to add its rules of thumb here.Go to the lesson →
09 · Binary search for boundariesPass this lesson to add its rules of thumb here.Go to the lesson →

Simple sorts

10 · Sorting and stabilityPass this lesson to add its rules of thumb here.Go to the lesson →
11 · Bubble sortPass this lesson to add its rules of thumb here.Go to the lesson →
12 · Selection sortPass this lesson to add its rules of thumb here.Go to the lesson →
13 · Insertion sortPass this lesson to add its rules of thumb here.Go to the lesson →
14 · Odd–even sortPass this lesson to add its rules of thumb here.Go to the lesson →
15 · Cycle sortPass this lesson to add its rules of thumb here.Go to the lesson →

Fast sorts

16 · Merge sortPass this lesson to add its rules of thumb here.Go to the lesson →
17 · Bottom-up merge sortPass this lesson to add its rules of thumb here.Go to the lesson →
18 · Quicksort: Lomuto partitionPass this lesson to add its rules of thumb here.Go to the lesson →
19 · Quicksort: choosing the pivotPass this lesson to add its rules of thumb here.Go to the lesson →
20 · Heaps and heap sortPass this lesson to add its rules of thumb here.Go to the lesson →
21 · Counting sortPass this lesson to add its rules of thumb here.Go to the lesson →
22 · Bucket sortPass this lesson to add its rules of thumb here.Go to the lesson →
23 · Choosing a sortPass this lesson to add its rules of thumb here.Go to the lesson →

Trees

24 · Trees: nodes, children and depthPass this lesson to add its rules of thumb here.Go to the lesson →
25 · Pre-order, in-order and post-orderPass this lesson to add its rules of thumb here.Go to the lesson →
26 · Level-order traversalPass this lesson to add its rules of thumb here.Go to the lesson →
27 · Are two trees the same?Pass this lesson to add its rules of thumb here.Go to the lesson →
28 · Binary search trees: search and insertPass this lesson to add its rules of thumb here.Go to the lesson →
29 · Deleting from a binary search treePass this lesson to add its rules of thumb here.Go to the lesson →
30 · Is it a binary search tree?Pass this lesson to add its rules of thumb here.Go to the lesson →
31 · Lowest common ancestor in a BSTPass this lesson to add its rules of thumb here.Go to the lesson →
32 · Lowest common ancestor in any binary treePass this lesson to add its rules of thumb here.Go to the lesson →

Graphs

33 · Graphs: vertices, edges and pathsPass this lesson to add its rules of thumb here.Go to the lesson →
34 · Storing a graph: matrix or listsPass this lesson to add its rules of thumb here.Go to the lesson →
35 · Depth-first searchPass this lesson to add its rules of thumb here.Go to the lesson →
36 · Breadth-first search: fewest hopsPass this lesson to add its rules of thumb here.Go to the lesson →
37 · Breadth-first search on a gridPass this lesson to add its rules of thumb here.Go to the lesson →
38 · Connected componentsPass this lesson to add its rules of thumb here.Go to the lesson →
39 · Topological sortPass this lesson to add its rules of thumb here.Go to the lesson →
40 · Finding a cycle in a directed graphPass this lesson to add its rules of thumb here.Go to the lesson →

Shortest paths & spanning trees

41 · Dijkstra’s algorithmPass this lesson to add its rules of thumb here.Go to the lesson →
42 · A*: search with a sense of directionPass this lesson to add its rules of thumb here.Go to the lesson →
43 · A* on the 8-puzzlePass this lesson to add its rules of thumb here.Go to the lesson →
44 · Bellman–Ford: why V − 1 roundsPass this lesson to add its rules of thumb here.Go to the lesson →
45 · Negative cyclesPass this lesson to add its rules of thumb here.Go to the lesson →
46 · Floyd–Warshall: every pair at oncePass this lesson to add its rules of thumb here.Go to the lesson →
47 · Minimum spanning trees: KruskalPass this lesson to add its rules of thumb here.Go to the lesson →
48 · Prim’s algorithmPass this lesson to add its rules of thumb here.Go to the lesson →

Greedy algorithms

49 · Activity selection: earliest finish firstPass this lesson to add its rules of thumb here.Go to the lesson →
50 · Making change: when greedy failsPass this lesson to add its rules of thumb here.Go to the lesson →
51 · Huffman codingPass this lesson to add its rules of thumb here.Go to the lesson →
52 · Minimising latenessPass this lesson to add its rules of thumb here.Go to the lesson →
53 · Offline caching: evict the furthestPass this lesson to add its rules of thumb here.Go to the lesson →

Dynamic programming

54 · Fibonacci: memoise or tabulatePass this lesson to add its rules of thumb here.Go to the lesson →
55 · Weighted job schedulingPass this lesson to add its rules of thumb here.Go to the lesson →
56 · The 0/1 knapsackPass this lesson to add its rules of thumb here.Go to the lesson →
57 · Longest common subsequencePass this lesson to add its rules of thumb here.Go to the lesson →
58 · Longest common substringPass this lesson to add its rules of thumb here.Go to the lesson →
59 · Edit distancePass this lesson to add its rules of thumb here.Go to the lesson →
60 · Longest increasing subsequencePass this lesson to add its rules of thumb here.Go to the lesson →
61 · Catalan numbersPass this lesson to add its rules of thumb here.Go to the lesson →
62 · Travelling salesman: brute force vs DPPass this lesson to add its rules of thumb here.Go to the lesson →
63 · Dynamic time warpingPass this lesson to add its rules of thumb here.Go to the lesson →

Strings & hashing

64 · Anagrams: sort or countPass this lesson to add its rules of thumb here.Go to the lesson →
65 · Hash functionsPass this lesson to add its rules of thumb here.Go to the lesson →
66 · Rabin–Karp: a rolling hashPass this lesson to add its rules of thumb here.Go to the lesson →
67 · KMP: the prefix tablePass this lesson to add its rules of thumb here.Go to the lesson →
68 · KMP: searching without backing upPass this lesson to add its rules of thumb here.Go to the lesson →

Numbers, matrices & geometry

69 · Pascal’s trianglePass this lesson to add its rules of thumb here.Go to the lesson →
70 · A matrix in a spiralPass this lesson to add its rules of thumb here.Go to the lesson →
71 · Fast powers and matrix exponentiationPass this lesson to add its rules of thumb here.Go to the lesson →
72 · Solving linear equationsPass this lesson to add its rules of thumb here.Go to the lesson →
73 · Solving non-linear equationsPass this lesson to add its rules of thumb here.Go to the lesson →
74 · Bresenham’s line algorithmPass this lesson to add its rules of thumb here.Go to the lesson →
75 · The fast Fourier transformPass this lesson to add its rules of thumb here.Go to the lesson →
76 · The inverse FFT: multiplying polynomialsPass this lesson to add its rules of thumb here.Go to the lesson →

Beyond the textbook model

77 · Online algorithms: pagingPass this lesson to add its rules of thumb here.Go to the lesson →
78 · Parallel algorithms: work and spanPass this lesson to add its rules of thumb here.Go to the lesson →
79 · Hard problems: approximate vertex coverPass this lesson to add its rules of thumb here.Go to the lesson →
80 · From pseudocode to a choicePass this lesson to add its rules of thumb here.Go to the lesson →