O(n) · visualised
Algorithm & data-structure visualizer
Watch classic algorithms and data structures run step by step. Pick one to start.
Sorting algorithms
Watch arrays sort themselves, step by step.
Bubble sortBubble sort repeatedly swaps adjacent out-of-order elements, letting the largest "bubble" to the end each pass.Insertion sortInsertion sort builds the sorted array one item at a time, inserting each new element into its place among the earlier ones.Selection sortSelection sort repeatedly finds the smallest remaining element and moves it to the front.Merge sortMerge sort splits the array in half, sorts each half, then merges the two sorted halves together.Quick sortQuick sort picks a pivot, partitions the array around it, then sorts the two sides.Heap sortHeap sort builds a max-heap, then repeatedly moves the largest element to the end.Shell sortShell sort is insertion sort that first compares far-apart elements, shrinking the gap each round.Cocktail sortCocktail sort is bubble sort that sweeps both ways — bubbling the largest up and the smallest down each round.Comb sortComb sort is bubble sort that compares elements a large gap apart, shrinking the gap toward 1.Gnome sortGnome sort steps forward while order is correct and swaps backward when it isn't.Odd-even sortOdd-even sort compares fixed pairs — first the odd-indexed pairs, then the even ones — repeating until none are out of order.Pancake sortPancake sort sorts using only prefix flips: bring the largest value to the front, then flip it down to its place.Cycle sortCycle sort places each value directly into its final spot, making the theoretical minimum number of writes.Stooge sortStooge sort is a recursive curiosity: sort the first 2/3, then the last 2/3, then the first 2/3 again.Counting sortCounting sort doesn't compare elements — it tallies how many of each value there are, then writes them back out in order. Linear time when the value range is small.Radix sortRadix sort sorts numbers one digit at a time, least-significant first, using a stable bucket pass for each digit. No comparisons needed.
Searching
Find a value in an array.
Linear searchLinear search scans the array left to right, comparing each element with the target until it finds it.Binary searchBinary search repeatedly halves a sorted array, comparing the middle element with the target.Jump searchJump search steps through a sorted array in fixed blocks, then scans linearly inside the block that may hold the target.Interpolation searchInterpolation search guesses where the target likely is by interpolating between the range's endpoints — fast on uniform data.Exponential searchExponential search doubles an index until it passes the target, then binary-searches the bracketed range.Ternary searchTernary search splits a sorted array into thirds, probing two points each step to discard two-thirds of what's left.Fibonacci searchFibonacci search narrows a sorted array using Fibonacci numbers — only addition and subtraction, no division.
String matching
Find a pattern inside a text.
KMP string matchingKnuth-Morris-Pratt finds every place a pattern occurs in a text without ever re-checking a character — on a mismatch it slides the pattern using a precomputed failure table.Rabin-Karp string matchingRabin-Karp compares a rolling hash of each text window against the pattern's hash, only checking characters when the hashes match — fast on average thanks to O(1) hash updates.Boyer-Moore string matchingBoyer-Moore compares the pattern from the right and, on a mismatch, jumps ahead by aligning the offending text character with its last spot in the pattern — often skipping many positions, which makes it fast in practice.
Selection
Find the k-th smallest value.
Data structures
How data is stored, added, and removed.
Stack (LIFO)A stack is a last-in, first-out (LIFO) structure: push adds to the top, pop removes the top.Queue (FIFO)A queue is a first-in, first-out (FIFO) structure: enqueue adds to the back, dequeue removes from the front.DequeA deque (double-ended queue) allows adding and removing from both ends.Binary search treeA binary search tree keeps values ordered: everything left of a node is smaller, everything right is larger — so lookups follow one path down.AVL treeAn AVL tree is a binary search tree that rebalances itself after every insert with rotations, so it can never degenerate into a slow chain — height stays logarithmic.Binary heap (min)A binary min-heap keeps the smallest value at the root; insert bubbles a value up and extract-min sinks the new root down.Linked listA linked list chains nodes together with pointers — O(1) to add at the head, but you must walk the chain to find anything.Hash tableA hash table maps each value to a bucket via a hash function, giving average O(1) lookup; collisions share a bucket as a chain.Trie (prefix tree)A trie stores words along shared paths from a root, one character per edge. Words with the same prefix share that prefix's nodes, so lookups cost only the word's length.Union-Find (DSU)A disjoint-set structure tracks a partition of items into groups, answering "are these two in the same set?" and merging sets — near-constant time per operation.
Tree algorithms
Walk a tree, node by node.
In-order traversalIn-order traversal visits the left subtree, then the node, then the right subtree — on a BST that yields sorted order.Pre-order traversalPre-order traversal visits the node first, then its left and right subtrees — handy for copying or serializing a tree.Post-order traversalPost-order traversal visits both subtrees first, then the node — handy for deleting or evaluating a tree.Level-order traversalLevel-order traversal visits the tree top to bottom, left to right — it's breadth-first search on a tree, using a queue.
Graph algorithms
Traversing nodes and edges.
BFSBreadth-first search explores a graph level by level using a FIFO queue.DFSDepth-first search explores as far as possible down each branch before backtracking, using a stack.Iterative-deepening DFSIterative-deepening DFS runs a depth-limited DFS over and over, raising the limit each round — getting BFS's level-by-level reach with DFS's tiny memory.Dijkstra (shortest path)Dijkstra finds the shortest path from a start node by always expanding the closest unvisited node — here edge cost is the distance between nodes.Prim's MSTPrim grows a minimum spanning tree from a start node, always adding the cheapest edge that reaches a new node.Kruskal's MSTKruskal builds a minimum spanning tree by adding edges from cheapest to most expensive, skipping any that would form a cycle.Borůvka's MSTBorůvka builds a minimum spanning tree in rounds: every component grabs its own cheapest outgoing edge at once, so the forest merges quickly in parallel.Bellman-FordBellman-Ford finds shortest paths by relaxing every edge V-1 times — slower than Dijkstra, but it handles negative edge weights.Topological sortA topological sort lines up the vertices of a directed acyclic graph so every edge points forward — a valid order to do tasks that depend on each other.Floyd-WarshallFloyd-Warshall finds the shortest distance between every pair of vertices at once, by repeatedly asking whether routing through one more vertex is shorter.Strongly connected componentsAn SCC is a group of vertices that can all reach each other. Kosaraju's algorithm finds them with two depth-first passes — one on the graph, one on its reverse.Maximum flowHow much can flow from a source to a sink through a network of capacitated pipes? Edmonds-Karp finds out by repeatedly pushing flow along the shortest path with spare room.Articulation points & bridgesAn articulation point is a vertex whose removal splits the graph; a bridge is such an edge. Tarjan finds both in one DFS by tracking how far back each subtree can reach.Connected componentsConnected components are the separate "islands" of a graph — maximal groups of vertices reachable from one another. A flood-fill colours each one.Bipartite checkA graph is bipartite if its vertices split into two groups with every edge crossing between them — checked by 2-colouring with BFS.Cycle detectionCycle detection asks whether an undirected graph contains a loop — DFS finds one the moment it meets a back edge to an earlier vertex.
Grid pathfinding
Route through a maze of walls.
BFS pathfindingBreadth-first search explores a grid in rings of equal distance, so the first time it reaches the goal it has found the shortest path — but it fans out in every direction.A* pathfindingA* is BFS with a sense of direction: it always expands the cell with the best estimated total cost, so it heads toward the goal and explores far fewer cells.Greedy best-firstGreedy best-first search always heads for the cell that looks closest to the goal. It's fast and direct, but it can be fooled into a longer, non-optimal path.Bidirectional searchBidirectional search runs two BFS waves at once — one from the start, one from the goal — and stops where they meet, exploring far fewer cells than a single search.Dijkstra (weighted grid)Dijkstra on a grid where cells cost different amounts to enter finds the cheapest route — which may wind around costly terrain instead of taking the fewest steps.Maze generationThe recursive backtracker carves a maze with a randomized depth-first walk — burrowing forward, knocking out walls, and backing up at dead ends until every cell is reached.Flood fillFlood fill is the paint bucket: from one cell it spreads outward to every connected open cell, stopping at walls — filling a whole region in one sweep.Maze — PrimRandomized Prim's grows a maze outward from a seed: it keeps a frontier of rooms next to the maze and keeps connecting a random one until every room is in.Maze — WilsonWilson's algorithm carves a maze with loop-erased random walks — wandering from unvisited rooms until they hit the maze — so every possible maze is equally likely.
Dynamic programming
Fill a table, reuse subproblems.
Edit distanceEdit distance (Levenshtein) is the fewest single-character inserts, deletes, and substitutions to turn one string into another — built up in a table.Longest common subsequenceThe longest common subsequence is the longest sequence of characters appearing in both strings in the same order, not necessarily adjacent — found with a DP table.Coin changeCoin change finds the fewest coins that add up to a target amount, with an unlimited supply of each denomination — built up in a table, amount by amount.Subset sumSubset sum asks whether any subset of a set of numbers adds up to a target — a table marks each (number, sum) reachable or not.Longest increasing subsequenceThe longest increasing subsequence is the longest run of values that increases left to right, picking elements in order but not necessarily adjacent.0/1 Knapsack0/1 knapsack picks the most valuable set of items that fits a weight budget — each item taken whole or not at all — filled in capacity by capacity.Unbounded knapsackUnbounded knapsack maximizes value within a weight budget when each item can be taken any number of times — so the best column to the left feeds the same row.PartitionThe partition problem asks whether a set of numbers can be split into two groups with equal sums — true only if the total is even and some subset reaches half of it.Kadane's max subarrayKadane's algorithm finds the largest sum of any contiguous slice in one pass — extending a running window, and abandoning it the moment its sum turns negative.Fibonacci (DP)The Fibonacci sequence built bottom-up: each term is the sum of the previous two, filling a one-row table left to right in linear time.
Computational geometry
Shapes and points in the plane.
Number theory
Primes, divisors, and number tricks.
Sieve of EratosthenesAn ancient, elegant way to find every prime up to n: list the numbers, then repeatedly take the next uncrossed one and strike out all its multiples.Euclid's GCDThe greatest common divisor of a and b, seen as geometry: the side of the largest square that tiles an a×b rectangle with no gaps.Euler's totient sieveφ(n) counts how many integers from 1 to n share no common factor with n. A sieve fills in every φ at once.Pascal's triangleEach entry is the sum of the two directly above it; the edges are 1. Row n lists the binomial coefficients C(n,0..n).Collatz conjecture (3n+1)From n, halve it if even, otherwise 3n+1. The unproven conjecture: you always eventually reach 1.Continued fractionWrite a/b as a₀ + 1/(a₁ + 1/(a₂ + …)). The terms are exactly the quotients of Euclid's algorithm.Prime factorization (factor tree)Repeatedly split a number into its smallest prime factor and the rest, until only primes remain at the leaves.