Skip to content
Ayotl

← All courses

Algorithms

Each lesson takes a classic algorithm, follows it step by step with a program you can change and run, and counts how much work it does. It starts with sorting, one of the things computers do most. It helps to know big O notation, from the first unit of data structures. Your progress is saved in this browser, no account needed.

0/11 lessons0 xp0 day streak

Unit 1Sorting

  1. 1
    Bubble sortCompare neighbors and swap them if they're out of order, pass after pass: the easiest sort to understand, and why it's O(n²).Start →
  2. Insertion sortLike sorting cards in your hand: each number slides left until it finds its place. O(n²), but nearly free if the list is already sorted.
  3. Selection sortFind the smallest and put it in front; then the next one. Always n(n - 1)/2 comparisons, but only n - 1 swaps.
  4. Merge sortSplit in half, sort each half with the same function and merge them: O(n log n), at the cost of extra memory.
  5. QuicksortPick a pivot, put the smaller ones to its left and repeat on each side: O(n log n) on average, with no extra memory; O(n²) with a bad pivot.

Unit 2Searching

  1. Binary searchLook at the middle one and throw away the half where it can't be: a million sorted items in 20 looks, O(log n).

Unit 3Traversal

  1. The maze: breadth-first search (BFS)Getting out of a maze by the shortest path with a queue: BFS explores in layers, and the first time it reaches the exit it's already along the path with the fewest steps.
  2. Flood fill: the paint bucketHow the paint bucket knows where to stop: a stack, four neighbors and a single rule, keep going while the cell is the old color.

Unit 4Simulation

  1. The Game of LifeA board that moves on its own: each cell counts its eight neighbors, and two lines of rules give you oscillators, gliders and even computers.

Unit 5Recursion

  1. The Tower of HanoiMoving a tower of disks from one peg to another with a function that calls itself: the base case, the call stack and why it takes 2ⁿ − 1 moves.

Unit 6Numbers

  1. The sieve of EratosthenesAll the primes up to n without a single division: crossing out the multiples of each prime from its square, why going up to √n is enough, and why it works almost as little as counting.