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

20
0/200 of 20 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

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

12
0/120 of 12 structures visited

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

Tools

8

Practice, calculate, and explore algorithms interactively