Visualize Sorting Algorithms Compared | QuantumSketch
Visualize sorting algorithms as bars that swap and settle: bubble sort crawls, quicksort partitions, merge sort divides. Side-by-side animation shows why speed differs.
Visualize sorting algorithms as bars that swap and settle: bubble sort crawls with adjacent swaps, quicksort partitions around pivots, and merge sort divides and merges. Racing them on identical input makes O(n²) vs O(n log n) obvious.
The setup
Represent the array as bars, height = value, shuffled. Color the bars being compared or moved. Then run each algorithm on the same input.
What each algorithm looks like
| Algorithm | Visual signature | Average cost | |---|---|---| | Bubble sort | Big values slowly "bubble" right | O(n²) | | Insertion sort | Left side grows sorted, one insert at a time | O(n²) | | Quicksort | Snaps into chunks around pivots | O(n log n) | | Merge sort | Splits, then merges runs — see merge sort | O(n log n) |
The race, beat by beat
- Show one shuffled array, duplicated into 2–3 panels.
- Start them together.
- Quicksort/merge finish while bubble sort is still mid-array.
- Display swap/comparison counters — the numbers diverge sharply.
- Land on the complexity classes — why the gap grows with n.
Seeing bubble sort still crawling while quicksort is done is the most convincing complexity lesson there is.
Manim building blocks
Rectangle/BarChart for bars, Transform/Swap for moves, Indicate to flash comparisons, and Integer counters. Pairs with Animate Binary Search for searching.
The prompt
"Race bubble sort, quicksort, and merge sort on the same 40-bar shuffled array, highlighting comparisons and showing swap counters."
→ Render it at quantumsketch.app.
Written by Shihab Shahriar Antor · Shahriar Labs
FAQ
Q.What's the best way to compare sorting algorithms visually?
Run them side by side on the same shuffled array of bars, where bar height equals value. Bubble sort repeatedly swaps adjacent out-of-order bars, so you see large values 'bubble' to the end slowly. Quicksort picks a pivot and partitions everything around it, then recurses, so the array snaps into roughly sorted chunks fast. Merge sort splits into halves and merges sorted runs. Watching all three race on identical input makes the difference between O(n²) and O(n log n) obvious — bubble sort is still crawling while quicksort and merge sort have finished.
Q.Why is bubble sort so much slower than quicksort in an animation?
Because bubble sort does roughly n² comparisons while quicksort averages about n log n. In an animation on, say, 50 bars, bubble sort makes thousands of adjacent swaps, inching the largest values rightward one position at a time, so it visibly takes far longer. Quicksort's partitioning moves many elements toward their final region in a single pass, so the array organizes in dramatically fewer steps. Seeing the swap counts diverge is a more convincing argument for algorithmic complexity than the formulas alone.