■ DSA Algorithms Cheat Sheet (with Hints)
Sorting Algorithms
• Bubble Sort: Repeatedly swap adjacent elements. O(n²).
• Selection Sort: Select min each pass. O(n²).
• Insertion Sort: Insert into sorted subarray. Good for small/near-sorted data.
• Merge Sort: Divide & merge. O(n log n). Stable.
• Quick Sort: Partition with pivot. O(n log n) avg, O(n²) worst.
• Heap Sort: Heapify + extract max. O(n log n).
• Counting Sort: Counts elements. O(n+k), integers only.
• Radix Sort: Sort by digits. O(nk).
• Bucket Sort: Distribute into buckets, then sort.
Searching Algorithms
• Linear Search: Sequential check. O(n).
• Binary Search: Sorted array required. O(log n).
• Jump Search: Jumps of √n. O(√n).
• Interpolation Search: Good for uniform distribution. O(log log n).
• Exponential Search: Expanding range + binary search. O(log n).
Greedy Algorithms
• Activity Selection: Pick max non-overlapping activities.
• Fractional Knapsack: Take items by value/weight ratio.
• Huffman Coding: Optimal prefix codes for compression.
• Prim’s MST: Grow MST by edges.
• Kruskal’s MST: Sort edges + union-find.
• Dijkstra’s: Shortest path, non-negative weights.
Dynamic Programming
• Fibonacci: Memoization or tabulation.
• 0/1 Knapsack: Pick/not pick each item.
• Coin Change: Ways or min coins.
• LCS: Longest common subsequence.
• LIS: Longest increasing subsequence.
• Matrix Chain Multiplication: Optimal parenthesization.
• Edit Distance: Min insert/delete/replace.
• Egg Dropping: Min trials with k eggs.
• Bellman-Ford: Shortest path, handles negatives.
• Floyd-Warshall: All-pairs shortest paths.
Graph Algorithms
• BFS: Level-order traversal.
• DFS: Depth-first traversal.
• Topological Sort: Ordering of DAG nodes.
• Tarjan’s Algorithm: Find strongly connected components.
• Kosaraju’s Algorithm: SCC using DFS twice.
• Kahn’s Algorithm: Topological sort with in-degree.
• A* Search: Heuristic-based shortest path.
String Algorithms
• Naive Search: Check each position.
• KMP: Prefix table to skip checks.
• Rabin-Karp: Hashing for substring search.
• Z Algorithm: Pattern preprocessing.
• Aho-Corasick: Multi-pattern search.
• Boyer-Moore: Skip mismatches efficiently.
• Manacher’s Algorithm: Longest palindrome in O(n).
Math & Number Theory
• Euclidean GCD: Divide until remainder = 0.
• LCM: lcm(a,b) = a*b/gcd(a,b).
• Sieve of Eratosthenes: Mark non-primes up to n.
• Modular Exponentiation: Fast power mod m.
• Chinese Remainder Theorem: Solve system of congruences.
• Karatsuba Multiplication: Fast divide & conquer multiply.
Tree Algorithms
• Traversals: Inorder, Preorder, Postorder, Level Order.
• AVL Tree: Self-balancing rotations.
• Segment Tree: Range queries & updates.
• Fenwick Tree: Efficient prefix sums.
• Trie: Prefix matching.
• LCA: Lowest common ancestor in tree.
Advanced / Misc
• Union-Find (DSU): Disjoint set operations.
• Ford-Fulkerson: Max flow in graph.
• Edmonds-Karp: BFS version of max flow.
• Hopcroft-Karp: Max bipartite matching.
• Suffix Array: Efficient substring operations.
• Suffix Automaton: Compact automaton for substrings.
• Bloom Filter: Probabilistic membership check.
• Skip List: Linked list with shortcuts.