CodeCookbook

Algorithm & Data Structure Cheat Sheet

Sorting Algorithms

NameTimeSpaceStableOnlineNotes
Adaptive SortO(n log n)O(log n)nonoReads the data before sorting: counting sort for integer ranges, insertion sort for tiny or nearly-sorted inputs, introsort with heapsort fallback otherwise.
Bubble SortO(n²)O(1)yesnoCompares adjacent pairs and swaps them if out of order. Each pass settles the largest remaining element.
Bucket SortO(n+k)O(n)yesnoScatters elements into buckets, sorts each bucket with insertion sort, then concatenates.
Cocktail SortO(n²)O(1)yesnoBidirectional bubble sort — forward pass pushes the max right, backward pass pushes the min left. Faster on partially-sorted data.
Comb SortO(n²)O(1)nonoEliminates turtles (small values near the end) by comparing elements far apart. Shrinks the gap by factor 1.3 each pass.
Counting SortO(n+k)O(k)yesnoTallies occurrences of each integer and reconstructs the array. Requires a bounded key range.
Cycle SortO(n²)O(1)nonoWrites each element exactly once by tracing permutation cycles. Optimal when array writes are expensive.
Gnome SortO(n²)O(1)yesyesA single pointer advances if neighbors are in order, or swaps and retreats. The simplest possible sorting algorithm.
Heap SortO(n log n)O(1)nonoBuilds a max-heap, then repeatedly extracts the max to produce sorted output in-place.
Insertion SortO(n²)O(1)yesyesInserts each element into its correct spot in the already-sorted prefix. Fast on nearly-sorted data.
IntrosortO(n log n)O(log n)nonoQuicksort with a safety net: if recursion depth exceeds 2·log₂n, it falls back to heapsort. Small subarrays finish with insertion sort. The basis of std::sort.
Logos SortO(n log n)O(log n)nonoNine strategies applied in order: tally, gallop, cut at the golden ratio, consult three voices, roll the die, divide into three, recurse the small.
Merge SortO(n log n)O(n)yesnoRecursively divides in half, sorts each half, then merges. Guaranteed O(n log n) in all cases.
Odd-Even SortO(n²)O(1)yesnoAlternates between odd-indexed and even-indexed adjacent swaps. Parallelizes naturally — each phase can run simultaneously.
Pancake SortO(n²)O(1)nonoSorts by prefix reversals only — like flipping stacks of pancakes. Each round places the next-largest element using at most 2 flips.
Quick SortO(n log n)O(log n)nonoPicks a pivot, partitions around it, and recurses on both sides. O(n log n) average; O(n²) worst case.
Radix SortO(nk)O(n+k)yesnoSorts digit by digit, least to most significant, using counting sort at each position.
Selection SortO(n²)O(1)nonoFinds the minimum of the unsorted region and swaps it to the sorted boundary. O(n) swaps total.
Shell SortO(n log² n)O(1)nonoInsertion sort over a shrinking gap — long-range swaps first, fine-tuning last.
Tim SortO(n log n)O(n)yesnoDetects natural runs, extends short ones with insertion sort, then merges via galloping mode. Python's and Java's built-in sort.

Data Structures

NameTimeSpaceNotes
AVL TreeO(log n)O(n)Self-balancing BST. Every insert/delete triggers LL, RR, LR, or RL rotations to keep balance factors in {−1, 0, 1}.
Binary HeapO(log n)O(n)Min-heap tree. Insert and extract-min with percolate operations.
BSTO(log n)O(n)Binary Search Tree. Insert, search, delete, and traversal animations.
DequeO(1)O(n)Double-ended queue. Push and pop from both front and back.
GraphO(V+E)O(V)Undirected graph with BFS and DFS traversal step-by-step.
Hash TableO(1) avgO(n)Hash function maps keys to buckets. Separate chaining for collisions.
Linked ListO(n)O(n)Nodes connected by pointers. Insert, delete, and traverse.
QueueO(1)O(n)First-In-First-Out. Enqueue at back, dequeue from the front.
Red-Black TreeO(log n)O(n)Self-balancing BST with color bits. Uses recoloring and rotations to maintain 5 invariants, guaranteeing O(log n) height.
Segment TreeO(log n)O(n)Range-query tree. O(log n) range-sum queries and point updates with step-by-step traversal animations.
StackO(1)O(n)Last-In-First-Out. Push and pop from the top of the stack.
TrieO(m)O(n·m)Prefix tree that stores strings character by character. O(m) insert and search where m is the word length.
Visualizer keyboard shortcuts: Spaceplay/pause  · step  · R reset