Reference Catalog

Algorithm Library

A practical index of the algorithms students need first: what each one does, when to reach for it, and the complexity curves to memorize.

15
Categories
65
Algorithms
65
Labs
Showing 65 of 65 algorithms
Concepts
Searching Beginner

Linear Search

Scan each element until the target is found or the list ends.

Time: O(n)
Space: O(1)
Searching Beginner

Binary Search

Compare the middle element, then discard the half that cannot contain the target.

Time: O(log n)
Space: O(1)
Searching Intermediate

Lower Bound

Find the first index whose value is greater than or equal to a target.

Time: O(log n)
Space: O(1)
Arrays Beginner

Two Pointers

Move two indexes through an array to shrink, expand, or compare a search region.

Time: O(n)
Space: O(1)
Arrays Intermediate

Sliding Window

Maintain a moving window and update its state as the boundary changes.

Time: O(n)
Space: O(1) to O(k)
Arrays Beginner

Prefix Sum

Store cumulative totals so range sums are answered by subtraction.

Time: O(n) build, O(1) query
Space: O(n)
Sorting Beginner

Bubble Sort

Repeatedly swap adjacent out-of-order elements until the largest values bubble right.

Time: O(n^2)
Space: O(1)
Sorting Beginner

Selection Sort

Select the smallest remaining value and place it at the current position.

Time: O(n^2)
Space: O(1)
Sorting Beginner

Insertion Sort

Grow a sorted prefix by inserting each new value into its correct place.

Time: O(n^2), best O(n)
Space: O(1)
Sorting Intermediate

Merge Sort

Divide the array, sort each half, then merge sorted halves.

Time: O(n log n)
Space: O(n)
Sorting Intermediate

Quick Sort

Partition values around a pivot, then recursively sort both sides.

Time: Average O(n log n), worst O(n^2)
Space: O(log n)
Sorting Intermediate

Heap Sort

Build a heap, then repeatedly move the max value to the end.

Time: O(n log n)
Space: O(1)
Sorting Intermediate

Counting Sort

Count each key, then rebuild output in key order.

Time: O(n + k)
Space: O(k)
Recursion Beginner

Recursion

Solve a problem by reducing it to smaller versions of itself.

Time: Depends on branches
Space: Call stack depth
Backtracking Intermediate

Backtracking

Build a candidate, recurse, then undo the choice when returning.

Time: Often exponential
Space: O(depth)
Backtracking Intermediate

Generate Subsets

For each item, branch into include and exclude decisions.

Time: O(2^n)
Space: O(n)
Backtracking Intermediate

Generate Permutations

Choose each unused value for the next position.

Time: O(n!)
Space: O(n)
Greedy Intermediate

Activity Selection

Sort by ending time and always take the compatible activity that finishes earliest.

Time: O(n log n)
Space: O(1)
Greedy Beginner

Merge Intervals

Sort intervals by start and combine overlapping neighbors.

Time: O(n log n)
Space: O(n)
Graphs Beginner

Breadth-First Search

Visit nodes level by level with a queue.

Time: O(V + E)
Space: O(V)
Graphs Beginner

Depth-First Search

Explore as far as possible before backtracking.

Time: O(V + E)
Space: O(V)
Graphs Intermediate

Topological Sort

Order directed acyclic graph nodes so prerequisites come first.

Time: O(V + E)
Space: O(V)
Graphs Intermediate

Dijkstra Shortest Path

Always expand the currently closest known node using a priority queue.

Time: O((V + E) log V)
Space: O(V)
Graphs Advanced

Bellman-Ford

Relax all edges repeatedly to handle negative weights and detect negative cycles.

Time: O(VE)
Space: O(V)
Graphs Advanced

Floyd-Warshall

Try every node as an intermediate stop between every pair of nodes.

Time: O(V^3)
Space: O(V^2)
Graphs Intermediate

Union Find

Track disjoint sets with parent pointers, path compression, and union by rank.

Time: Almost O(1)
Space: O(n)
Graphs Intermediate

Kruskal MST

Sort edges by weight and add edges that connect different components.

Time: O(E log E)
Space: O(V)
Graphs Intermediate

Prim MST

Grow a tree by repeatedly adding the cheapest edge leaving the current tree.

Time: O(E log V)
Space: O(V)
Trees Beginner

Tree Traversals

Visit tree nodes in preorder, inorder, postorder, or level order.

Time: O(n)
Space: O(h)
Trees Beginner

BST Search

Use the binary-search-tree ordering to move left or right from each node.

Time: O(h)
Space: O(1)
Trees Intermediate

Heap Push and Pop

Maintain the heap property by bubbling values up or down.

Time: O(log n)
Space: O(1)
Dynamic Programming Beginner

Memoized Fibonacci

Cache repeated recursive results so each state is solved once.

Time: O(n)
Space: O(n)
Dynamic Programming Intermediate

0/1 Knapsack

For each item and capacity, choose between skipping or taking the item.

Time: O(nW)
Space: O(nW) or O(W)
Dynamic Programming Intermediate

Longest Increasing Subsequence

Track the smallest possible tail value for increasing subsequences of each length.

Time: O(n log n)
Space: O(n)
Dynamic Programming Intermediate

Longest Common Subsequence

Compare prefixes of two strings and reuse overlap between states.

Time: O(nm)
Space: O(nm)
Dynamic Programming Intermediate

Coin Change

Build answers for smaller amounts, then extend them with each coin.

Time: O(amount * coins)
Space: O(amount)
Strings Advanced

KMP String Search

Precompute prefix information so mismatches skip redundant comparisons.

Time: O(n + m)
Space: O(m)
Strings Intermediate

Rabin-Karp

Compare rolling hashes of fixed-size windows against the pattern hash.

Time: Average O(n + m)
Space: O(1)
Strings Intermediate

Trie Insert and Search

Store strings as paths through character nodes.

Time: O(length)
Space: O(total characters)
Math Beginner

Euclidean GCD

Repeatedly replace the larger number with its remainder.

Time: O(log min(a, b))
Space: O(1)
Math Beginner

Sieve of Eratosthenes

Mark multiples of each prime as composite.

Time: O(n log log n)
Space: O(n)
Math Intermediate

Fast Exponentiation

Square the base and consume exponent bits from right to left.

Time: O(log exponent)
Space: O(1)
Arrays Beginner

Array Traversal

Visit every element once from left to right.

Time: O(n)
Space: O(1)
Arrays Beginner

Find Minimum and Maximum

Track the best value seen so far while scanning the array once.

Time: O(n)
Space: O(1)
Arrays Beginner

Reverse Array

Swap values from both ends while moving inward.

Time: O(n)
Space: O(1)
Arrays Beginner

Rotate Array

Move elements by k positions, often using three reversals.

Time: O(n)
Space: O(1)
Arrays Beginner

Remove Duplicates From Sorted Array

Use a write pointer to keep only the first copy of each value.

Time: O(n)
Space: O(1)
Hashing Beginner

Two Sum

Store seen values in a hash map and look for the complement of each value.

Time: O(n)
Space: O(n)
Strings Beginner

Palindrome Check

Compare characters from both ends while moving inward.

Time: O(n)
Space: O(1)
Strings Beginner

Valid Anagram

Count characters in one string and subtract counts using the other string.

Time: O(n)
Space: O(k)
Strings Beginner

First Non-Repeating Character

Count frequencies, then scan again to find the first count of one.

Time: O(n)
Space: O(k)
Stacks and Queues Beginner

Valid Parentheses

Push opening brackets and match each closing bracket with the most recent opener.

Time: O(n)
Space: O(n)
Stacks and Queues Beginner

Next Greater Element

Use a monotonic stack to find the next bigger value to the right.

Time: O(n)
Space: O(n)
Stacks and Queues Beginner

Queue With Two Stacks

Use one stack for incoming values and one stack for outgoing values.

Time: Amortized O(1)
Space: O(n)
Linked Lists Beginner

Linked List Traversal

Follow next pointers until the list ends.

Time: O(n)
Space: O(1)
Linked Lists Beginner

Insert at Linked List Head

Point the new node to the old head, then move head to the new node.

Time: O(1)
Space: O(1)
Linked Lists Beginner

Reverse Linked List

Walk the list and reverse each next pointer.

Time: O(n)
Space: O(1)
Linked Lists Beginner

Middle of Linked List

Move a slow pointer one step and a fast pointer two steps.

Time: O(n)
Space: O(1)
Matrices Beginner

Matrix Traversal

Visit each cell using nested row and column loops.

Time: O(R*C)
Space: O(1)
Matrices Beginner

Matrix Neighbors

Use direction arrays to visit valid neighboring cells.

Time: O(1) per cell
Space: O(1)
Matrices Beginner

Flood Fill

Starting from one cell, visit connected cells with the same value and recolor them.

Time: O(R*C)
Space: O(R*C)
Trees Beginner

Level Order Traversal

Use a queue to visit tree nodes one level at a time.

Time: O(n)
Space: O(n)
Trees Beginner

Tree Height

Height is one plus the maximum height of the left and right subtrees.

Time: O(n)
Space: O(h)
Recursion Beginner

Factorial

Multiply n by the factorial of n - 1 until the base case.

Time: O(n)
Space: O(n)
Math Beginner

Sum of Digits

Repeatedly take the last digit and divide the number by ten.

Time: O(number of digits)
Space: O(1)