Algorithms
31 algorithms, each with a short explanation, its steps and invariants, a complexity table, when to use it, an interactive lab you can step through, and the same implementation in C#, Python, Java and C++, tested against shared cases.
Searching
- Linear search
Check each element from the start until one matches. O(n), no preconditions, and the right choice for small or unsorted data.
O(n)
- Binary search
Halve a sorted range with every comparison: O(log n) to find a value or the first position where it could go.
O(log n)
- Binary search on the answer
Binary search over possible answers instead of array positions, when a yes/no feasibility check is monotonic in the answer.
O(n log S)
- Jump, exponential and interpolation search
Three searches on sorted data: jump search in √n blocks, exponential search with a doubling bound, and interpolation search that guesses by value.
jump O(√n); exponential O(log i); interpolation O(log log n) expected on uniform keys
Sorting and selection
- Bubble sort
Swap adjacent out-of-order pairs, pass after pass, until a pass makes no swap. Stable and in place, O(n²) in general, O(n) on sorted input.
O(n²) worst, O(n) sorted
- Insertion sort
Grow a sorted prefix by inserting the next element into place. Stable, in place, O(n²) worst case and very fast on small or nearly sorted input.
O(n²) worst, O(n) sorted
- Selection sort
Find the minimum of the unsorted part and swap it to the front. Θ(n²) comparisons on every input, at most n − 1 swaps, not stable.
Θ(n²) comparisons, at most n − 1 swaps
- Merge sort
Split in half, sort each half, merge the two sorted halves. Θ(n log n) on every input, stable, with O(n) extra memory.
Θ(n log n)
- Quicksort
Partition around a pivot, then sort each side. O(n log n) expected, O(n²) worst case, in place, not stable.
O(n log n) expected, O(n²) worst
- Heap sort
Build a max-heap in place, then repeatedly swap the root to the end and sift down. O(n log n) worst case, O(1) extra space, not stable.
O(n log n), O(1) extra
- Quickselect
Partition like quicksort but keep only the side that holds index k: the k-th smallest element in O(n) expected time, in place.
O(n) expected, O(n²) worst
Trees
- Binary tree traversal
Preorder, inorder and postorder visit a binary tree depth-first with the call stack; level order visits it breadth-first with a queue.
Θ(n)
Graphs
- BFS and DFS
Breadth-first search explores a graph in rings of increasing distance with a queue; depth-first search follows one path as far as it goes. Both run in O(V + E).
O(V + E)
- Topological sort
Order the vertices of a directed acyclic graph so every edge points forward. Kahn’s algorithm repeatedly removes vertices with no incoming edges.
O(V + E)
- Dijkstra’s shortest paths
Single-source shortest paths with non-negative edge weights: settle the closest unsettled vertex, relax its edges, repeat. O((V + E) log V) with a binary heap.
O((V + E) log V)
- Kruskal’s minimum spanning tree
Sort the edges by weight and add each one unless it would close a cycle, checked with union-find: a minimum spanning tree in O(E log E).
O(E log E)
Recursion and backtracking
- Recursion and the call stack
A function that calls itself on a smaller input, with a base case that stops it. Each call is a stack frame; depth decides memory and stack overflow.
O(e) vs O(log e) calls
- Backtracking
Build a solution one choice at a time, explore, then undo the choice: subsets and permutations as a walk over a decision tree.
O(n · 2ⁿ) subsets, O(n · n!) permutations
Dynamic programming
- Fibonacci: recursion to dynamic programming
Naive recursion recomputes the same subproblems exponentially often; memoization or a table computes each once. The smallest example of dynamic programming.
Θ(φⁿ) naive, O(n) memo or table
- 0/1 knapsack
Pick items of given weights and values to maximize value within a capacity, each item at most once. A 2D table in O(nW) pseudo-polynomial time.
O(nW) time and space (pseudo-polynomial)
Greedy
- Interval scheduling and merging
Two greedy sweeps after one sort: pick the most non-overlapping intervals by earliest end, or merge overlapping intervals by start.
O(n log n)
Hashing
- Hash table with open addressing
A hash table that stores keys directly in its slot array and probes for the next free slot on a collision: linear, quadratic or double hashing.
expected O(1) with a good hash and bounded load; O(n) worst
Strings
- String matching: naive and KMP
Find every occurrence of a pattern in a text: the naive scan tries each alignment in O(nm); Knuth–Morris–Pratt never moves back in the text, O(n + m).
naive O(n·m) worst; KMP O(n + m)
- UTF-8 and UTF-16 encoding
How one Unicode code point becomes 1–4 UTF-8 bytes or 1–2 UTF-16 code units, and why string length depends on what you count.
O(n) in code points
Bits and math
- Bit operations
Set, clear, toggle and test single bits, isolate the lowest set bit, count bits and shift, on a fixed-width integer with its two’s-complement reading.
O(1) per operation; popcount O(set bits)
- Euclid’s GCD
The greatest common divisor by repeated remainders, gcd(a, b) = gcd(b, a mod b), in O(log min(a, b)) steps; the LCM follows from it.
O(log min(a, b))
- Sieve of Eratosthenes
Find every prime up to n by crossing out the multiples of each prime, starting from its square: O(n log log n) time and O(n) memory.
O(n log log n) time, O(n) space
Array techniques
- Two pointers
Two indices that move through an array under a rule: pair sums from both ends of a sorted array, or in-place filtering with read and write pointers.
O(n)
- Sliding window
Maintain a contiguous window and update its summary as it slides: fixed-size window sums, or the longest window under a limit, in O(n).
O(n)
- Prefix sums and difference arrays
Precompute running totals once, then answer any range sum in O(1); the difference array does the reverse for range updates.
build O(n), query O(1)
- Matrix traversal
Walk a 2D array row by row or column by column, and see why the order matters: row-major storage makes rows contiguous and columns strided.
Θ(rows · cols)
Data structure operations
These operations are covered, with their lab and code, on the data structure’s own page.
Memory model simulations
How values, references, frames and buffers behave in each language; these live in the Memory Lab.
Want to see two algorithms race on the same input? Try the comparisons. Solved, visualized problems are in the problem bank.