HomeAll tracks · pricing · account
Python11 apps · Python, SQL & data
Start free Log in

BitSize Python · Algorithms

Watch algorithms think.

Every classic algorithm, animated: drive it and watch the Python run.

Track your progress

Save where you are and see what you remember.

Sign up free →
Part 1

Thinking in steps

0/6

What an algorithm is, counting its steps, why one loop is O(n), two nested loops O(n²) and halving O(log n), Big-O / Ω / Θ, and how fast each growth rate gets out of hand.

  1. 01 What an algorithm isLargest value · O(n) Up next
  2. 02 One loop: counting stepsCount above a limit · O(n) Free account
  3. 03 Nested loops: O(n²)Every pair · O(n²) Free account
  4. 04 Halving: O(log n)Halving (guess the number) · O(log n) 🔒 Subscribe
  5. 05 Big-O, Big-Ω and Big-ΘO, Ω and Θ 🔒 Subscribe
  6. 06 Comparing growthGrowth rates 🔒 Subscribe
Part 2
0/3

Linear search and its best, average and worst cases; binary search on a sorted list; and binary search for boundaries: first, last and where to insert.

  1. 07 Linear search: best, average and worst caseLinear search · O(n) 🔒 Subscribe
  2. 08 Binary searchBinary search · O(log n) 🔒 Subscribe
  3. 09 Binary search for boundariesLower and upper bound · O(log n) 🔒 Subscribe
Part 3

Simple sorts

0/6

What sorting and stability mean, then bubble, selection, insertion, odd–even and cycle sort: quadratic, but each with a lesson to teach.

  1. 10 Sorting and stabilitySorting & stability 🔒 Subscribe
  2. 11 Bubble sortBubble sort · O(n²) 🔒 Subscribe
  3. 12 Selection sortSelection sort · O(n²) 🔒 Subscribe
  4. 13 Insertion sortInsertion sort · O(n²) 🔒 Subscribe
  5. 14 Odd–even sortOdd–even sort · O(n²) 🔒 Subscribe
  6. 15 Cycle sortCycle sort · O(n²) 🔒 Subscribe
Part 4

Fast sorts

0/8

Merge sort top-down and bottom-up, quicksort and its pivots, heaps and heap sort, counting and bucket sort, and how to choose a sort.

  1. 16 Merge sortMerge sort · O(n log n) 🔒 Subscribe
  2. 17 Bottom-up merge sortBottom-up merge sort · O(n log n) 🔒 Subscribe
  3. 18 Quicksort: Lomuto partitionQuicksort (Lomuto) · O(n log n) avg 🔒 Subscribe
  4. 19 Quicksort: choosing the pivotPivot rules · O(n log n) avg 🔒 Subscribe
  5. 20 Heaps and heap sortHeap sort · O(n log n) 🔒 Subscribe
  6. 21 Counting sortCounting sort · O(n + k) 🔒 Subscribe
  7. 22 Bucket sortBucket sort · O(n) avg, if spread 🔒 Subscribe
  8. 23 Choosing a sortChoosing a sort 🔒 Subscribe
Part 5

Trees

0/9

Nodes and depth, the three depth-first orders and level order, comparing trees, binary search trees (search, insert, delete, validate) and lowest common ancestors.

  1. 24 Trees: nodes, children and depthTree basics · O(n) 🔒 Subscribe
  2. 25 Pre-order, in-order and post-orderDepth-first traversals · O(n) 🔒 Subscribe
  3. 26 Level-order traversalLevel-order traversal · O(n) 🔒 Subscribe
  4. 27 Are two trees the same?Same tree · O(n) 🔒 Subscribe
  5. 28 Binary search trees: search and insertBinary search tree · O(h) 🔒 Subscribe
  6. 29 Deleting from a binary search treeBST delete · O(h) 🔒 Subscribe
  7. 30 Is it a binary search tree?BST check · O(n) 🔒 Subscribe
  8. 31 Lowest common ancestor in a BSTLCA (BST) · O(h) 🔒 Subscribe
  9. 32 Lowest common ancestor in any binary treeLCA (any binary tree) · O(n) 🔒 Subscribe
Part 6

Graphs

0/8

Vertices and edges, matrices vs adjacency lists, depth-first and breadth-first search, grids, connected components, topological sort and cycles.

  1. 33 Graphs: vertices, edges and pathsGraph basics 🔒 Subscribe
  2. 34 Storing a graph: matrix or listsMatrix vs lists 🔒 Subscribe
  3. 35 Depth-first searchDFS · O(V + E) 🔒 Subscribe
  4. 36 Breadth-first search: fewest hopsBFS · O(V + E) 🔒 Subscribe
  5. 37 Breadth-first search on a gridGrid BFS · O(rows × cols) 🔒 Subscribe
  6. 38 Connected componentsComponents · O(V + E) 🔒 Subscribe
  7. 39 Topological sortTopological sort · O(V + E) 🔒 Subscribe
  8. 40 Finding a cycle in a directed graphCycle detection · O(V + E) 🔒 Subscribe
Part 7

Shortest paths & spanning trees

0/8

Dijkstra, A* (on a maze and the 8-puzzle), Bellman–Ford and negative cycles, Floyd–Warshall, and minimum spanning trees with Kruskal and Prim.

  1. 41 Dijkstra’s algorithmDijkstra · O((V + E) log V) 🔒 Subscribe
  2. 42 A*: search with a sense of directionA* · O(E log V) 🔒 Subscribe
  3. 43 A* on the 8-puzzleA* (8-puzzle) · depends on h 🔒 Subscribe
  4. 44 Bellman–Ford: why V − 1 roundsBellman–Ford · O(V × E) 🔒 Subscribe
  5. 45 Negative cyclesNegative cycle · O(V × E) 🔒 Subscribe
  6. 46 Floyd–Warshall: every pair at onceFloyd–Warshall · O(n³) 🔒 Subscribe
  7. 47 Minimum spanning trees: KruskalKruskal · O(E log E) 🔒 Subscribe
  8. 48 Prim’s algorithmPrim · O(E log V) 🔒 Subscribe
Part 8

Greedy algorithms

0/5

Take the best-looking step and never look back: activity selection, making change (and when greedy fails), Huffman coding, lateness and caching.

  1. 49 Activity selection: earliest finish firstActivity selection · O(n log n) 🔒 Subscribe
  2. 50 Making change: when greedy failsCoin change · O(amount × coins) 🔒 Subscribe
  3. 51 Huffman codingHuffman · O(n log n) 🔒 Subscribe
  4. 52 Minimising latenessEarliest deadline first · O(n log n) 🔒 Subscribe
  5. 53 Offline caching: evict the furthestBélády · O(n² × k) 🔒 Subscribe
Part 9

Dynamic programming

0/10

Solve each small problem once: Fibonacci, job scheduling, knapsack, LCS, edit distance, LIS, Catalan numbers, the travelling salesman and time warping.

  1. 54 Fibonacci: memoise or tabulateFibonacci · O(n) 🔒 Subscribe
  2. 55 Weighted job schedulingWeighted jobs · O(n log n) 🔒 Subscribe
  3. 56 The 0/1 knapsack0/1 knapsack · O(n × cap) 🔒 Subscribe
  4. 57 Longest common subsequenceLCS · O(m × n) 🔒 Subscribe
  5. 58 Longest common substringCommon substring · O(m × n) 🔒 Subscribe
  6. 59 Edit distanceLevenshtein distance · O(m × n) 🔒 Subscribe
  7. 60 Longest increasing subsequenceLIS · O(n²) 🔒 Subscribe
  8. 61 Catalan numbersCatalan numbers · O(n²) 🔒 Subscribe
  9. 62 Travelling salesman: brute force vs DPHeld–Karp · O(n² 2ⁿ) 🔒 Subscribe
  10. 63 Dynamic time warpingDTW · O(n × m) 🔒 Subscribe
Part 10

Strings & hashing

0/5

Anagrams, what makes a good hash function, Rabin–Karp’s rolling hash, and KMP’s prefix table for searching text without backing up.

  1. 64 Anagrams: sort or countAnagram check · O(k log k) 🔒 Subscribe
  2. 65 Hash functionsHashing · O(1) average 🔒 Subscribe
  3. 66 Rabin–Karp: a rolling hashRabin–Karp · O(n + m) expected 🔒 Subscribe
  4. 67 KMP: the prefix tableKMP prefix function · O(m) 🔒 Subscribe
  5. 68 KMP: searching without backing upKnuth–Morris–Pratt · O(n + m) 🔒 Subscribe
Part 11

Numbers, matrices & geometry

0/8

Pascal’s triangle, spiral matrices, fast powers, solving equations, drawing a line with integers, and the fast Fourier transform.

  1. 69 Pascal’s trianglePascal’s triangle · O(n²) 🔒 Subscribe
  2. 70 A matrix in a spiralSpiral matrix · O(rows × cols) 🔒 Subscribe
  3. 71 Fast powers and matrix exponentiationExponentiation by squaring · O(log n) 🔒 Subscribe
  4. 72 Solving linear equationsGaussian elimination · O(n³) 🔒 Subscribe
  5. 73 Solving non-linear equationsBisection and Newton · O(log(1/tol)) 🔒 Subscribe
  6. 74 Bresenham’s line algorithmBresenham · O(max(|dx|, |dy|)) 🔒 Subscribe
  7. 75 The fast Fourier transformFFT (Cooley–Tukey) · O(n log n) 🔒 Subscribe
  8. 76 The inverse FFT: multiplying polynomialsFFT multiplication · O(n log n) 🔒 Subscribe
Part 12

Beyond the textbook model

0/4

When you can’t see the future (online paging), have many processors (work and span), or the problem is hard (approximation), and choosing an algorithm.

  1. 77 Online algorithms: pagingLRU vs OPT · O(n) for LRU 🔒 Subscribe
  2. 78 Parallel algorithms: work and spanGreedy scheduling · max(W/p, S) … W/p + S 🔒 Subscribe
  3. 79 Hard problems: approximate vertex coverVertex cover · O(V + E) 🔒 Subscribe
  4. 80 From pseudocode to a choiceFind a repeat 🔒 Subscribe