Help us improve
Share bugs, ideas, or general feedback.
From algorithm-expert
Comprehensive reference on algorithm families, data structures, complexity analysis, optimization techniques, and decision frameworks for selecting the right algorithm for any computational problem
npx claudepluginhub trusted-american/marketplace --plugin algorithm-expertHow this skill is triggered — by the user, by Claude, or both
Slash command
/algorithm-expert:algorithmsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Master reference for algorithm selection, complexity analysis, and optimization across all major computational domains.
Implements and selects optimal data structures—hash tables (chaining, Robin Hood), balanced BSTs (AVL, Red-Black), heaps, tries, skip lists, segment/Fenwick trees, Bloom filters, Union-Find—for performance constraints, custom collections, memory optimization.
Big O notation, time/space complexity analysis, and choosing efficient algorithms.
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Share bugs, ideas, or general feedback.
Master reference for algorithm selection, complexity analysis, and optimization across all major computational domains.
| Class | Name | Example | 1K items | 1M items |
|---|---|---|---|---|
| O(1) | Constant | Hash lookup | 1 op | 1 op |
| O(log n) | Logarithmic | Binary search | 10 ops | 20 ops |
| O(√n) | Square root | Trial division | 31 ops | 1,000 ops |
| O(n) | Linear | Array scan | 1,000 ops | 1,000,000 ops |
| O(n log n) | Linearithmic | Merge sort | 10,000 ops | 20,000,000 ops |
| O(n√n) | — | Mo's algorithm | 31,623 ops | 1,000,000,000 ops |
| O(n^2) | Quadratic | Nested loops | 1,000,000 ops | 10^12 ops |
| O(n^3) | Cubic | Matrix multiply (naive) | 10^9 ops | 10^18 ops |
| O(2^n) | Exponential | Subset enumeration | 10^301 ops | — |
| O(n!) | Factorial | Permutation enumeration | — | — |
Some operations are expensive occasionally but cheap on average:
| More Space → | Less Time |
|---|---|
| Hash table (O(n) space) | O(1) lookup (vs O(n) scan) |
| Prefix sum array (O(n) space) | O(1) range sum (vs O(n) recompute) |
| DP table (O(n×m) space) | O(n×m) time (vs exponential recursion) |
| Inverted index (O(n) space) | O(1) text search (vs O(n) scan) |
| Memoization cache (O(n) space) | Avoid recomputation |
| Algorithm | Best | Average | Worst | Space | Stable | Notes |
|---|---|---|---|---|---|---|
| Timsort | O(n) | O(n log n) | O(n log n) | O(n) | Yes | Python/Java default. Exploits existing runs. |
| Merge sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | Predictable. Great for linked lists. |
| Quicksort | O(n log n) | O(n log n) | O(n^2) | O(log n) | No | Fastest in practice (cache-friendly). |
| Heapsort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | In-place, guaranteed O(n log n). |
| Introsort | O(n log n) | O(n log n) | O(n log n) | O(log n) | No | C++ std::sort. Quicksort → heapsort fallback. |
| Insertion sort | O(n) | O(n^2) | O(n^2) | O(1) | Yes | Best for nearly-sorted or tiny (n < 20). |
| Shell sort | O(n log n) | O(n^{4/3}) | O(n^{3/2}) | O(1) | No | In-place, low overhead. |
| Algorithm | Time | Space | When to Use |
|---|---|---|---|
| Counting sort | O(n + k) | O(k) | Integer keys in small range [0, k) |
| Radix sort | O(n × d) | O(n + k) | Integers or fixed-length strings, d digits |
| Bucket sort | O(n + k) | O(n + k) | Uniformly distributed floating-point |
Is n < 20?
→ Insertion sort (low overhead, cache-friendly)
Is data nearly sorted?
→ Timsort or insertion sort
Are keys integers in bounded range?
→ Counting sort or radix sort
Need stability?
→ Merge sort or Timsort
Need in-place?
→ Heapsort (guaranteed) or introsort (practical)
Default?
→ Use the language's built-in sort (usually Timsort or introsort)
| Algorithm | Time | Space | Prerequisite | Best For |
|---|---|---|---|---|
| Linear search | O(n) | O(1) | None | Unsorted, small data |
| Binary search | O(log n) | O(1) | Sorted data | Sorted arrays |
| Hash table lookup | O(1) avg | O(n) | Hash table built | Exact key lookup |
| BST search | O(log n) avg | O(n) | Balanced tree | Ordered data + range queries |
| Interpolation search | O(log log n) avg | O(1) | Sorted + uniform | Uniformly distributed numeric keys |
| Exponential search | O(log n) | O(1) | Sorted | Unbounded/infinite lists |
| Trie lookup | O(k) | O(alphabet × n × k) | Trie built | Prefix search, autocomplete |
| Bloom filter | O(k) | O(m bits) | Filter built | Approximate membership (set containment) |
| B-tree search | O(log_B n) | O(n) | B-tree built | Disk-based / database indexes |
Need exact key lookup?
→ Hash table (O(1) average)
Need ordered iteration + search?
→ Balanced BST or B-tree (O(log n))
Need prefix matching?
→ Trie (O(k) where k = key length)
Need approximate membership?
→ Bloom filter (O(1), false positives only)
Data is sorted array?
→ Binary search (O(log n))
Data is small or unsorted?
→ Linear search (O(n), but cache-friendly)
| Algorithm | Time | Space | Use Case |
|---|---|---|---|
| BFS | O(V+E) | O(V) | Shortest path (unweighted), level-order |
| DFS | O(V+E) | O(V) | Connectivity, cycle detection, topological sort |
| Algorithm | Time | Space | Use Case |
|---|---|---|---|
| Dijkstra | O((V+E) log V) | O(V) | Non-negative weights, single source |
| Bellman-Ford | O(V×E) | O(V) | Negative weights, negative cycle detection |
| Floyd-Warshall | O(V^3) | O(V^2) | All-pairs shortest path |
| A* | O(E) best case | O(V) | Single target with heuristic (maps, games) |
| Johnson's | O(V^2 log V + VE) | O(V^2) | All-pairs, sparse graph, some negative weights |
| Algorithm | Time | Use Case |
|---|---|---|
| Kruskal's | O(E log E) | Sparse graphs (edge list natural) |
| Prim's | O((V+E) log V) | Dense graphs (adjacency list/matrix) |
| Borůvka's | O(E log V) | Parallel-friendly |
| Problem | Algorithm | Time |
|---|---|---|
| Topological sort | Kahn's (BFS) or DFS | O(V+E) |
| Strongly connected components | Tarjan's or Kosaraju's | O(V+E) |
| Articulation points / bridges | Tarjan's | O(V+E) |
| Maximum flow | Ford-Fulkerson / Dinic's | O(VE^2) / O(V^2E) |
| Bipartite matching | Hopcroft-Karp | O(E√V) |
| Cycle detection | DFS with coloring | O(V+E) |
| Euler path/circuit | Hierholzer's | O(E) |
A problem has optimal substructure AND overlapping subproblems:
| Pattern | Problems | Recurrence Shape |
|---|---|---|
| Linear DP | Fibonacci, climbing stairs, house robber | dp[i] = f(dp[i-1], dp[i-2]) |
| Knapsack | 0/1 knapsack, subset sum, coin change | dp[i][w] = max(include, exclude) |
| LCS/Edit distance | Longest common subsequence, diff | dp[i][j] = f(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) |
| Interval DP | Matrix chain, balloon burst | dp[i][j] = min over k of (dp[i][k] + dp[k][j]) |
| Tree DP | Tree diameter, subtree problems | dp[node] = f(dp[children]) |
| Bitmask DP | TSP, assignment, Hamiltonian path | dp[mask][i] = f(dp[mask^bit][j]) |
| Digit DP | Count numbers with property | dp[pos][tight][state] |
| Algorithm | Time | Use Case |
|---|---|---|
| KMP | O(n + m) | Single pattern matching |
| Rabin-Karp | O(n + m) avg | Multiple pattern matching, rolling hash |
| Aho-Corasick | O(n + m + z) | Multiple pattern matching simultaneously |
| Boyer-Moore | O(n/m) best | Fast single pattern (long patterns) |
| Suffix array | O(n log n) build, O(m log n) query | Multiple queries on same text |
| Suffix tree | O(n) build | Substring queries, LCP, repeat finding |
| Z-algorithm | O(n + m) | Pattern matching, period finding |
| Manacher's | O(n) | Longest palindromic substring |
| Levenshtein distance | O(n × m) | Edit distance, fuzzy matching |
| Structure | Access | Search | Insert | Delete | Notes |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | Cache-friendly, fixed size |
| Dynamic array | O(1) | O(n) | O(1)* | O(n) | Amortized append |
| Linked list | O(n) | O(n) | O(1)† | O(1)† | Good for frequent insert/delete at known position |
| Deque | O(1) | O(n) | O(1)‡ | O(1)‡ | Double-ended, front/back ops |
| Skip list | O(log n) | O(log n) | O(log n) | O(log n) | Probabilistic, concurrent-friendly |
| Structure | Lookup | Insert | Delete | Notes |
|---|---|---|---|---|
| Hash map | O(1) avg | O(1) avg | O(1) avg | Most common key-value store |
| Hash set | O(1) avg | O(1) avg | O(1) avg | Membership testing |
| Bloom filter | O(k) | O(k) | N/A | Probabilistic membership, very space-efficient |
| Cuckoo hash | O(1) worst | O(1) avg | O(1) | Guaranteed worst-case lookup |
| Swiss table | O(1) avg | O(1) avg | O(1) avg | SIMD-accelerated, Google's absl |
| Structure | Search | Insert | Delete | Notes |
|---|---|---|---|---|
| BST | O(h) | O(h) | O(h) | h = log n balanced, n worst |
| AVL tree | O(log n) | O(log n) | O(log n) | Strictly balanced, fast lookup |
| Red-Black tree | O(log n) | O(log n) | O(log n) | Relaxed balance, fast insert/delete |
| B-tree | O(log_B n) | O(log_B n) | O(log_B n) | Disk-optimized, database indexes |
| Trie | O(k) | O(k) | O(k) | k = key length. Prefix queries. |
| Segment tree | O(log n) | O(log n) | — | Range queries + point updates |
| Fenwick tree | O(log n) | O(log n) | — | Prefix sums, simpler than segment tree |
| Structure | Find-min | Insert | Decrease-key | Delete-min | Merge |
|---|---|---|---|---|---|
| Binary heap | O(1) | O(log n) | O(log n) | O(log n) | O(n) |
| Fibonacci heap | O(1) | O(1)* | O(1)* | O(log n)* | O(1) |
| Pairing heap | O(1) | O(1) | O(log n)* | O(log n)* | O(1) |
*amortized
| Algorithm | Purpose | Error | Space |
|---|---|---|---|
| Bloom filter | Set membership | False positives only | O(m bits) |
| Count-Min Sketch | Frequency estimation | Overestimates | O(w × d) |
| HyperLogLog | Cardinality estimation | ~2% error | O(m bytes) |
| MinHash | Set similarity (Jaccard) | Configurable | O(k per set) |
| Locality-Sensitive Hashing | Approx nearest neighbor | Configurable | O(L × n × k) |
| Reservoir sampling | Uniform sample from stream | Exact | O(k) |
| Morris counting | Approximate counting | ~30% error | O(log log n) bits |
| Pattern | Use Case | Key Consideration |
|---|---|---|
| Map-Reduce | Independent per-element operations | Embarrassingly parallel |
| Fork-Join | Divide-and-conquer problems | Work-stealing for load balance |
| Pipeline | Multi-stage processing | Stage throughput balancing |
| Producer-Consumer | Decoupled generation/processing | Queue backpressure |
| Read-Copy-Update (RCU) | Read-heavy concurrent data | Near-zero read overhead |
| Lock-free queue | High-throughput messaging | CAS-based, ABA problem |
| Concurrent hash map | Shared key-value store | Striped locking or lock-free |
| Parallel prefix sum | Cumulative operations | O(n/p + log p) with p processors |
What type of problem is this? (sorting, searching, graph, string, optimization, etc.)
Theory and practice diverge for: