Useful Algorithms
A toolbox of must-know algorithms — searching, sorting, and graph traversal — built up step by step so you can reproduce them from memory.
These aren’t tied to a single LeetCode problem — they’re the building blocks that show up across hundreds of them. Learn each one until you can type it out without thinking.
Binary Search
Halve the search space each step to find a value in a sorted array in O(log n).
Quick Sort
Partition around a pivot, then recurse on each side. Average O(n log n), in place.
Depth-First Search (DFS)
Explore a graph by diving as deep as possible before backtracking.
Breadth-First Search (BFS)
Explore a graph level by level with a queue — the basis for shortest paths on unweighted graphs.
Dijkstra's Shortest Path
Shortest paths from a source on a weighted graph with non-negative edges, using a min-heap.
Topological Sort (Kahn's Algorithm)
Order the nodes of a DAG so every edge points forward — great for dependency resolution.
Union-Find (Disjoint Set Union)
Track connected groups with near-constant-time merges and membership checks.