How to Animate Merge Sort Step by Step | QuantumSketch
Animate merge sort by splitting an array into halves until single elements, then merging sorted pairs back up. The recursion tree makes O(n log n) visible.
Animate merge sort by splitting the array into halves until each piece is a single element, then merging sorted runs back together. The split-down, merge-up recursion tree makes the O(n log n) cost visible.
The two phases
Merge sort is divide and conquer:
- Divide (going down): split the array in half, recursively, until every subarray has one element (trivially sorted).
- Conquer (going up): merge adjacent sorted runs by repeatedly taking the smaller front element, until the whole array is one sorted run.
The animation, beat by beat
- Show the array as labeled bars:
[38, 27, 43, 3, 9, 82, 10]. - Split โ the array divides, halves drift apart, recursively, to singletons.
- Merge โ two sorted runs combine; highlight the two front elements being compared, place the smaller.
- Walk back up the tree until fully sorted.
- Annotate the depth โ logโn levels, each doing n work โ n log n.
Why the tree shape teaches complexity
| Level | Work | |---|---| | Each level | merges total n elements | | Number of levels | logโ n | | Total | n log n |
Seeing log n levels each costing n is how complexity stops being a memorized formula.
Manim building blocks
Rectangle/BarChart for values, Transform for moving bars during merges, Indicate to flash the compared pair, and Text labels for indices. Compare strategies in Visualize Sorting Algorithms and Animate Binary Search.
The prompt
"Animate merge sort on [38, 27, 43, 3, 9, 82, 10]: split into halves down to singletons, then merge back up, highlighting each comparison."
โ Render it at quantumsketch.app.
Written by Shihab Shahriar Antor ยท Shahriar Labs
FAQ
Q.What's the clearest way to animate merge sort?
Show the recursion as a tree that splits going down and merges going up. Start with the full array of bars, then animate it splitting in half, each half splitting again, until every piece is a single element. Then animate the merge phase: pairs of sorted runs combine by comparing front elements and placing the smaller one first, working back up the tree until the whole array is sorted. Coloring the active subarray and highlighting each comparison makes the divide-and-conquer structure obvious and shows why merge sort does about n log n comparisons.
Q.How do I animate sorting algorithms without coding them?
Describe the algorithm and the array to an AI animation tool: 'Animate merge sort on [38, 27, 43, 3, 9, 82, 10], splitting into halves and merging back, highlighting comparisons.' QuantumSketch generates a narrated Manim animation with bars representing values, color changes for active elements, and smooth movement during merges. Manim's bar charts, transforms, and indicate animations handle the visuals, so you focus on which algorithm and which input array best teach the idea.