BitSize Python · Algorithms
Watch algorithms think.
Every classic algorithm, animated: drive it and watch the Python run.
Thinking in steps
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.
- 01 What an algorithm isLargest value · O(n) Up next
- 02 One loop: counting stepsCount above a limit · O(n) Free account
- 03 Nested loops: O(n²)Every pair · O(n²) Free account
- 04 Halving: O(log n)Halving (guess the number) · O(log n) 🔒 Subscribe
- 05 Big-O, Big-Ω and Big-ΘO, Ω and Θ 🔒 Subscribe
- 06 Comparing growthGrowth rates 🔒 Subscribe
Searching
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.
Simple sorts
What sorting and stability mean, then bubble, selection, insertion, odd–even and cycle sort: quadratic, but each with a lesson to teach.
Fast sorts
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.
- 16 Merge sortMerge sort · O(n log n) 🔒 Subscribe
- 17 Bottom-up merge sortBottom-up merge sort · O(n log n) 🔒 Subscribe
- 18 Quicksort: Lomuto partitionQuicksort (Lomuto) · O(n log n) avg 🔒 Subscribe
- 19 Quicksort: choosing the pivotPivot rules · O(n log n) avg 🔒 Subscribe
- 20 Heaps and heap sortHeap sort · O(n log n) 🔒 Subscribe
- 21 Counting sortCounting sort · O(n + k) 🔒 Subscribe
- 22 Bucket sortBucket sort · O(n) avg, if spread 🔒 Subscribe
- 23 Choosing a sortChoosing a sort 🔒 Subscribe
Trees
Nodes and depth, the three depth-first orders and level order, comparing trees, binary search trees (search, insert, delete, validate) and lowest common ancestors.
- 24 Trees: nodes, children and depthTree basics · O(n) 🔒 Subscribe
- 25 Pre-order, in-order and post-orderDepth-first traversals · O(n) 🔒 Subscribe
- 26 Level-order traversalLevel-order traversal · O(n) 🔒 Subscribe
- 27 Are two trees the same?Same tree · O(n) 🔒 Subscribe
- 28 Binary search trees: search and insertBinary search tree · O(h) 🔒 Subscribe
- 29 Deleting from a binary search treeBST delete · O(h) 🔒 Subscribe
- 30 Is it a binary search tree?BST check · O(n) 🔒 Subscribe
- 31 Lowest common ancestor in a BSTLCA (BST) · O(h) 🔒 Subscribe
- 32 Lowest common ancestor in any binary treeLCA (any binary tree) · O(n) 🔒 Subscribe
Graphs
Vertices and edges, matrices vs adjacency lists, depth-first and breadth-first search, grids, connected components, topological sort and cycles.
- 33 Graphs: vertices, edges and pathsGraph basics 🔒 Subscribe
- 34 Storing a graph: matrix or listsMatrix vs lists 🔒 Subscribe
- 35 Depth-first searchDFS · O(V + E) 🔒 Subscribe
- 36 Breadth-first search: fewest hopsBFS · O(V + E) 🔒 Subscribe
- 37 Breadth-first search on a gridGrid BFS · O(rows × cols) 🔒 Subscribe
- 38 Connected componentsComponents · O(V + E) 🔒 Subscribe
- 39 Topological sortTopological sort · O(V + E) 🔒 Subscribe
- 40 Finding a cycle in a directed graphCycle detection · O(V + E) 🔒 Subscribe
Shortest paths & spanning trees
Dijkstra, A* (on a maze and the 8-puzzle), Bellman–Ford and negative cycles, Floyd–Warshall, and minimum spanning trees with Kruskal and Prim.
- 41 Dijkstra’s algorithmDijkstra · O((V + E) log V) 🔒 Subscribe
- 42 A*: search with a sense of directionA* · O(E log V) 🔒 Subscribe
- 43 A* on the 8-puzzleA* (8-puzzle) · depends on h 🔒 Subscribe
- 44 Bellman–Ford: why V − 1 roundsBellman–Ford · O(V × E) 🔒 Subscribe
- 45 Negative cyclesNegative cycle · O(V × E) 🔒 Subscribe
- 46 Floyd–Warshall: every pair at onceFloyd–Warshall · O(n³) 🔒 Subscribe
- 47 Minimum spanning trees: KruskalKruskal · O(E log E) 🔒 Subscribe
- 48 Prim’s algorithmPrim · O(E log V) 🔒 Subscribe
Greedy algorithms
Take the best-looking step and never look back: activity selection, making change (and when greedy fails), Huffman coding, lateness and caching.
- 49 Activity selection: earliest finish firstActivity selection · O(n log n) 🔒 Subscribe
- 50 Making change: when greedy failsCoin change · O(amount × coins) 🔒 Subscribe
- 51 Huffman codingHuffman · O(n log n) 🔒 Subscribe
- 52 Minimising latenessEarliest deadline first · O(n log n) 🔒 Subscribe
- 53 Offline caching: evict the furthestBélády · O(n² × k) 🔒 Subscribe
Dynamic programming
Solve each small problem once: Fibonacci, job scheduling, knapsack, LCS, edit distance, LIS, Catalan numbers, the travelling salesman and time warping.
- 54 Fibonacci: memoise or tabulateFibonacci · O(n) 🔒 Subscribe
- 55 Weighted job schedulingWeighted jobs · O(n log n) 🔒 Subscribe
- 56 The 0/1 knapsack0/1 knapsack · O(n × cap) 🔒 Subscribe
- 57 Longest common subsequenceLCS · O(m × n) 🔒 Subscribe
- 58 Longest common substringCommon substring · O(m × n) 🔒 Subscribe
- 59 Edit distanceLevenshtein distance · O(m × n) 🔒 Subscribe
- 60 Longest increasing subsequenceLIS · O(n²) 🔒 Subscribe
- 61 Catalan numbersCatalan numbers · O(n²) 🔒 Subscribe
- 62 Travelling salesman: brute force vs DPHeld–Karp · O(n² 2ⁿ) 🔒 Subscribe
- 63 Dynamic time warpingDTW · O(n × m) 🔒 Subscribe
Strings & hashing
Anagrams, what makes a good hash function, Rabin–Karp’s rolling hash, and KMP’s prefix table for searching text without backing up.
- 64 Anagrams: sort or countAnagram check · O(k log k) 🔒 Subscribe
- 65 Hash functionsHashing · O(1) average 🔒 Subscribe
- 66 Rabin–Karp: a rolling hashRabin–Karp · O(n + m) expected 🔒 Subscribe
- 67 KMP: the prefix tableKMP prefix function · O(m) 🔒 Subscribe
- 68 KMP: searching without backing upKnuth–Morris–Pratt · O(n + m) 🔒 Subscribe
Numbers, matrices & geometry
Pascal’s triangle, spiral matrices, fast powers, solving equations, drawing a line with integers, and the fast Fourier transform.
- 69 Pascal’s trianglePascal’s triangle · O(n²) 🔒 Subscribe
- 70 A matrix in a spiralSpiral matrix · O(rows × cols) 🔒 Subscribe
- 71 Fast powers and matrix exponentiationExponentiation by squaring · O(log n) 🔒 Subscribe
- 72 Solving linear equationsGaussian elimination · O(n³) 🔒 Subscribe
- 73 Solving non-linear equationsBisection and Newton · O(log(1/tol)) 🔒 Subscribe
- 74 Bresenham’s line algorithmBresenham · O(max(|dx|, |dy|)) 🔒 Subscribe
- 75 The fast Fourier transformFFT (Cooley–Tukey) · O(n log n) 🔒 Subscribe
- 76 The inverse FFT: multiplying polynomialsFFT multiplication · O(n log n) 🔒 Subscribe
Beyond the textbook model
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.