CodeCookbook

The Complete Guide

CodeCookbook

Algorithms & Data Structures

Interactive, step-by-step visualizations of algorithms and data structures. Learn by watching, not just reading.

Tools

Measure performance and watch algorithms race head-to-head

Sorting Algorithms

21
0/210 of 21 sorting visited

See how different strategies sort data step by step

Logos Sort

unstableoffline

Nine strategies applied in order: tally, gallop, cut at the golden ratio, consult three voices, roll the die, divide into three, recurse the small.

Time
O(n log n)
Space
O(log n)
View Recipe

Adaptive Sort

unstableoffline

Reads the data before sorting: counting sort for integer ranges, insertion sort for tiny or nearly-sorted inputs, introsort with heapsort fallback otherwise.

Time
O(n log n)
Space
O(log n)
View Recipe

Pattern-Defeating Quicksort

unstableoffline

Introsort hybrid hardened against adversarial input. Median-of-3 / ninther pivot, partial-insertion-sort early-out, equal-elements fast path, heapsort fallback. Default sort in Rust's std.

Time
O(n log n)
Space
O(log n)
View Recipe

Introsort

unstableoffline

Quicksort 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.

Time
O(n log n)
Space
O(log n)
View Recipe

Bubble Sort

stableoffline

Compares adjacent pairs and swaps them if out of order. Each pass settles the largest remaining element.

Time
O(n²)
Space
O(1)
View Recipe

Selection Sort

unstableoffline

Finds the minimum of the unsorted region and swaps it to the sorted boundary. O(n) swaps total.

Time
O(n²)
Space
O(1)
View Recipe

Insertion Sort

stableonline

Inserts each element into its correct spot in the already-sorted prefix. Fast on nearly-sorted data.

Time
O(n²)
Space
O(1)
View Recipe

Merge Sort

stableoffline

Recursively divides in half, sorts each half, then merges. Guaranteed O(n log n) in all cases.

Time
O(n log n)
Space
O(n)
View Recipe

Quick Sort

unstableoffline

Picks a pivot, partitions around it, and recurses on both sides. O(n log n) average; O(n²) worst case.

Time
O(n log n)
Space
O(log n)
View Recipe

Heap Sort

unstableoffline

Builds a max-heap, then repeatedly extracts the max to produce sorted output in-place.

Time
O(n log n)
Space
O(1)
View Recipe

Shell Sort

unstableoffline

Insertion sort over a shrinking gap — long-range swaps first, fine-tuning last.

Time
O(n log² n)
Space
O(1)
View Recipe

Counting Sort

stableoffline

Tallies occurrences of each integer and reconstructs the array. Requires a bounded key range.

Time
O(n+k)
Space
O(k)
View Recipe

Radix Sort

stableoffline

Sorts digit by digit, least to most significant, using counting sort at each position.

Time
O(nk)
Space
O(n+k)
View Recipe

Bucket Sort

stableoffline

Scatters elements into buckets, sorts each bucket with insertion sort, then concatenates.

Time
O(n+k)
Space
O(n)
View Recipe

Tim Sort

stableoffline

Detects natural runs, extends short ones with insertion sort, then merges via galloping mode. Python's and Java's built-in sort.

Time
O(n log n)
Space
O(n)
View Recipe

Cocktail Sort

stableoffline

Bidirectional bubble sort — forward pass pushes the max right, backward pass pushes the min left. Faster on partially-sorted data.

Time
O(n²)
Space
O(1)
View Recipe

Comb Sort

unstableoffline

Eliminates turtles (small values near the end) by comparing elements far apart. Shrinks the gap by factor 1.3 each pass.

Time
O(n²)
Space
O(1)
View Recipe

Gnome Sort

stableonline

A single pointer advances if neighbors are in order, or swaps and retreats. The simplest possible sorting algorithm.

Time
O(n²)
Space
O(1)
View Recipe

Pancake Sort

unstableoffline

Sorts by prefix reversals only — like flipping stacks of pancakes. Each round places the next-largest element using at most 2 flips.

Time
O(n²)
Space
O(1)
View Recipe

Cycle Sort

unstableoffline

Writes each element exactly once by tracing permutation cycles. Optimal when array writes are expensive.

Time
O(n²)
Space
O(1)
View Recipe

Odd-Even Sort

stableoffline

Alternates between odd-indexed and even-indexed adjacent swaps. Parallelizes naturally — each phase can run simultaneously.

Time
O(n²)
Space
O(1)
View Recipe

Data Structures

13
0/130 of 13 structures visited

Push, pop, enqueue, dequeue — watch every operation live

Stack

Last-In-First-Out. Push and pop from the top of the stack.

Time
O(1)
Space
O(n)
View Recipe

Queue

First-In-First-Out. Enqueue at back, dequeue from the front.

Time
O(1)
Space
O(n)
View Recipe

Deque

Double-ended queue. Push and pop from both front and back.

Time
O(1)
Space
O(n)
View Recipe

Linked List

Nodes connected by pointers. Insert, delete, and traverse.

Time
O(n)
Space
O(n)
View Recipe

Binary Heap

Min/max heap tree. Insert, extract-root, peek, heapify, and full heap-sort animation with percolate operations.

Time
O(log n)
Space
O(n)
View Recipe

2-3 Tree

Self-balancing search tree with 2-nodes (1 key, 2 children) and 3-nodes (2 keys, 3 children). Splits propagate up to keep height perfectly balanced.

Time
O(log n)
Space
O(n)
View Recipe

Hash Table

Hash function maps keys to buckets. Separate chaining for collisions.

Time
O(1) avg
Space
O(n)
View Recipe

BST

Binary Search Tree. Insert, search, delete, and traversal animations.

Time
O(log n)
Space
O(n)
View Recipe

Graph

Undirected graph with BFS and DFS traversal step-by-step.

Time
O(V+E)
Space
O(V)
View Recipe

AVL Tree

Self-balancing BST. Every insert/delete triggers LL, RR, LR, or RL rotations to keep balance factors in {−1, 0, 1}.

Time
O(log n)
Space
O(n)
View Recipe

Red-Black Tree

Self-balancing BST with color bits. Uses recoloring and rotations to maintain 5 invariants, guaranteeing O(log n) height.

Time
O(log n)
Space
O(n)
View Recipe

Trie

Prefix tree that stores strings character by character. O(m) insert and search where m is the word length.

Time
O(m)
Space
O(n·m)
View Recipe

Segment Tree

Range-query tree. O(log n) range-sum queries and point updates with step-by-step traversal animations.

Time
O(log n)
Space
O(n)
View Recipe

Tools

8

Practice, calculate, and explore algorithms interactively