Quick Sort Bar Chart Animation
Loading video...
Quick Sort Visualization Specification (Default Assumptions)
1. Overview
Create a Manim animation that visualizes the Quick Sort algorithm using a bar chart representation. The animation will demonstrate the selection of pivots, the partitioning process, recursive calls, and the final sorted array. All visual and timing parameters follow the default assumptions.
2. Data Setup
- Number of elements: 20 bars.
- Values: Random integers uniformly drawn from the range 1 – 100.
- Bar width: 0.4 units (adjusted automatically to fit the scene).
- Initial ordering: Random (unsorted).
3. Visual Elements
| Element | Description | Default Color |
|---|---|---|
| Bars (unsorted) | Rectangular bars representing array values. | TEAL (#008080) |
| Pivot bar | The bar currently chosen as pivot during a partition step. | RED (#FF0000) |
| Comparing bars | Bars being compared to the pivot during partition. | ORANGE (#FFA500) |
| Swapped bars | Bars that are swapped during partition. | GREEN (#00FF00) |
| Current sub‑array background | A faint rectangle behind the active sub‑array to indicate the recursion scope. | LIGHT_GRAY with 30 % opacity |
| Sorted bars | Bars after the entire array is sorted. | BLUE (#0000FF) |
| On‑screen text | Simple captions such as "Choosing pivot", "Partitioning", "Recursive call", and "Sorted!" displayed at the top center. | WHITE |
4. Animation Flow & Timing
| Step | Action | Duration |
|---|---|---|
| Setup | Generate random values, create bars, and display them in a line. | 1.0 s |
| Recursive Call Start | Fade‑in a light‑gray rectangle covering the current sub‑array and display caption "Recursive call on indices l … r". | 0.8 s |
| Pivot Selection | Highlight the pivot bar (rightmost element of the sub‑array) in RED and show caption "Choosing pivot (value = …)". | 0.8 s |
| Partition Loop | For each element i in the sub‑array (excluding pivot): |
- Highlight bar
iin ORANGE (comparison) → 0.5 s - If
i≤ pivot, swap it with the element at the partition indexj(highlight both in GREEN) → 0.5 s (swap animation) |
| Pivot Placement | After the loop, swap the pivot into its final position (highlight both bars in GREEN) → 0.5 s, then change the pivot bar to BLUE (sorted portion). |
| Recursive Calls | Recursively apply the same steps to the left and right sub‑arrays. Each recursive level follows the same timing pattern. |
| Completion | Once the whole array is sorted, change all bars to BLUE, display caption "Sorted!" and hold for 2.0 s. |
Note: The total animation length depends on the random data but typically ranges between 30 – 45 seconds.
5. Camera Settings
- Camera: 2‑D orthographic view only (no 3‑D camera or perspective). Static (no zoom or pan).
- Scene dimensions: Width 14 units, height 8 units, centered at the origin.
- Background: Dark gray (
#202020).
6. Text & Labels
- No numeric labels on individual bars (to keep the visual clean).
- Captions appear at the top center using a large, bold sans‑serif font.
- Optional: a small footer displaying "Quick Sort – O(n \log n) average case" after the final "Sorted!" caption.
7. Output
- File format: MP4 video (
quick_sort_default.mp4). - Resolution: 1920 × 1080 (HD).
- Frame rate: 30 fps.
8. Implementation Sketch (Manim Python Pseudocode)
from manim import *
import random
class QuickSortBars(Scene):
def construct(self):
# 1. Generate data
values = [random.randint(1, 100) for _ in range(20)]
max_val = max(values)
# 2. Create bars
bars = VGroup(*[
Rectangle(
width=0.4,
height=v / max_val * 5, # scale to fit scene height
fill_color=TEAL,
fill_opacity=1,
stroke_width=0,
).move_to(np.array([i*0.5 - 5, 0, 0]))
for i, v in enumerate(values)
])
self.play(FadeIn(bars, shift=UP), run_time=1)
# 3. Start recursive quick sort visualisation
self.quick_sort(bars, 0, len(values)-1)
# 4. Final highlight
self.play(*[bar.animate.set_fill(BLUE) for bar in bars], run_time=1)
sorted_text = Text("Sorted!", color=WHITE).to_edge(UP)
self.play(Write(sorted_text), run_time=2)
self.wait(2)
def quick_sort(self, bars, l, r, depth=0):
if l >= r:
return
# Highlight current sub‑array
sub_rect = Rectangle(
width=(r-l)*0.5+0.5,
height=5.5,
stroke_color=LIGHT_GRAY,
stroke_width=2,
fill_opacity=0.2,
fill_color=LIGHT_GRAY,
).move_to(bars[l].get_center() + (bars[r].get_center() - bars[l].get_center())/2)
caption = Text(f"Recursive call on indices {l} … {r}", color=WHITE).to_edge(UP)
self.play(FadeIn(sub_rect, scale=0.5), Write(caption), run_time=0.8)
# Choose pivot (rightmost)
pivot_bar = bars[r]
self.play(pivot_bar.animate.set_fill(RED), run_time=0.8)
pivot_val = values[r]
# Partition
i = l - 1
for j in range(l, r):
# compare
self.play(bars[j].animate.set_fill(ORANGE), run_time=0.5)
if values[j] <= pivot_val:
i += 1
# swap bars i and j
if i != j:
self.play(
Swap(bars[i], bars[j]),
bars[i].animate.set_fill(GREEN),
bars[j].animate.set_fill(GREEN),
run_time=0.5,
)
# swap values in list
values[i], values[j] = values[j], values[i]
else:
self.play(bars[i].animate.set_fill(GREEN), run_time=0.5)
# reset color after comparison
self.play(bars[j].animate.set_fill(TEAL), run_time=0.2)
# place pivot in final position
i += 1
self.play(Swap(bars[i], bars[r]), run_time=0.5)
values[i], values[r] = values[r], values[i]
self.play(bars[i].animate.set_fill(BLUE), run_time=0.3)
# Clean up sub‑array highlight
self.play(FadeOut(sub_rect), FadeOut(caption), run_time=0.5)
# Recurse left and right
self.quick_sort(bars, l, i-1, depth+1)
self.quick_sort(bars, i+1, r, depth+1)
The above code is a high‑level sketch; actual implementation may require adjustments for proper indexing, animation synchronization, and object references.
9. Summary
This specification provides a complete, ready‑to‑implement description for a Quick Sort visualization using default assumptions. The animation emphasizes algorithmic steps through color changes, captions, and a clean bar‑chart layout, culminating in a fully sorted array highlighted in blue. The camera is explicitly 2‑D only, with no 3‑D perspective or camera movements.
Created By
Description
A 2‑D Manim animation that visualizes Quick Sort on 20 random bars. It highlights pivot selection, comparison, swaps, and recursive sub‑array calls using distinct colors and captions, ending with all bars turned blue to show the sorted array.
Created At
Dec 31, 2025, 10:55 AM