Sorting, Side by Side
Six classic sorting algorithms race on the same 96 values as glowing 3D rings: real step-by-step code, live comparison and write counters, a finish order, and new data each round (random, nearly sorted, reversed, few unique).
Made in Claude Code (Claude subscription) with claude-opus-5.5. The agent wrote code, looked at the preview and fixed it over several iterations. This is not a single-shot result: comparisons on this site show models in one attempt.
Starting prompt
Build "Sorting, Side by Side": a live data visualisation in which six classic sorting algorithms race on the same data at the same time, drawn as glowing 3D rings. It should look like a premium, editorial data-viz piece and be beautiful from the first frame. Single self-contained HTML file; Three.js and its addons from a CDN via an import map are fine (Google Fonts too); build everything procedurally, with no external models, images or other fetches.
The algorithms (real, never faked):
- Bubble sort (with a shrinking bound and early exit), insertion sort (shifting values right), selection sort, top-down merge sort through an auxiliary buffer, quicksort (Hoare partition around the middle value) and heapsort (build a max-heap, then swap the root to the end and sift down).
- Write each one as a JavaScript generator that really sorts its own copy of the array and yields one event per primitive operation, with the indices involved: a comparison, an array write, a swap (two writes) or a copy into the merge buffer.
- All six lanes get a copy of the same 96 values. A shared clock counts ticks, where one tick is one comparison or one array write. Every frame the global tick grows, and each lane runs its generator until it has spent that many ticks. The race is fair, and the fast algorithms visibly finish first.
- Before each race, run all six to completion on a copy to count their ticks. Set the tick rate so that the slowest lane finishes after about 28 seconds.
One lane:
- A ring of 96 wedge-shaped bars standing on a dark, glossy disc, with element 0 at the back and the rest running clockwise. Each bar starts at an inner radius of about a third of the ring, and both its radial length and its height grow with its value. A sorted lane becomes a smooth spiral staircase.
- Colour encodes value with a perceptual OKLCH ramp: indigo-violet for low values, through magenta and coral, to amber-gold for high values. Even the lowest values stay clearly visible against the background.
- The rings are tilted about 45–50° towards the viewer (more top-down in portrait) and seen through an orthographic camera. They breathe gently in tilt and sway a little around their axis.
- Shading is a custom shader with:
- a key light and a fill light, with darker bar bases;
- faint reflections on the disc, clipped to its edge;
- faint guide circles and a thin progress arc around the rim.
- Elements being compared light up icy white for a moment, and written or swapped elements flash brighter. The glow fades within a fraction of a second, so each algorithm's pattern of activity is easy to read: a sweeping pair, converging pointers, a growing sorted region.
- Use gentle bloom only on these highlights and on the finish rings.
Layout: the six rings sit in a 3×2 grid on landscape screens and a 2×3 grid in portrait. The quadratic sorts take the first row and the n log n sorts the second. Under each ring show:
- the algorithm name;
- its complexity (O(n²) or O(n log n));
- live counts of comparisons and writes;
- on larger screens, the percentage of values already in their final place.
The race takes about 33 seconds per round, then it loops forever:
1. Deal (about 1.5 s): a wave runs around every ring, and the bars dip and rise to the new values.
2. Race: all six run together. When a lane finishes:
- a bright band sweeps once around its ring;
- its rim arc turns gold, silver or bronze (soft grey for the rest);
- a large serif ordinal (1st, 2nd, …) appears in the centre of the ring.
3. When all six are sorted, a short shared sweep runs around every ring, the status line names the winner, and the next round deals new data.
The data pattern changes every round, cycling through:
- a random shuffle;
- nearly sorted (a dozen values nudged a few places);
- reversed;
- few unique (only five distinct values).
The caption names the round and the pattern, so the different winners make sense: insertion sort wins on nearly sorted data, quicksort on the others.
Interface:
- A deep navy background with a soft vignette.
- Top left: the title in an elegant serif and a caption with the round, the data pattern and a short note about it.
- Top right: the global tick counter and a status line ("racing", "3 of 6 sorted · Quicksort first", "all sorted · Quicksort wins").
- A small colour legend bottom left. Bottom right, the hint "1 tick = 1 comparison or 1 array write · tap a lane to focus".
- Tapping a lane enlarges it smoothly and shows a one-sentence description of the algorithm. The other five shrink into a column (landscape) or a strip (portrait), and tapping again returns to the grid.
- Everything starts automatically and is visibly moving from the first frame, without any interaction. The layout works in a frame of about 1150×700 and on a phone (390×844, touch).
Engineering rules (the page must work on the first load, with no fixes):
- Use Three.js r170 and only methods that exist on each of its classes. Give every custom ShaderMaterial `fog: false`, and declare every uniform, constant and varying its shaders use.
- Declare each identifier once. Inside one calculation, never reuse a shared scratch vector for two values that are both still needed; use local vectors.
- Guard every animated value against NaN.
- The page runs in a sandboxed iframe without same-origin access, so do not use localStorage, sessionStorage, cookies, alert or network fetches.
- Answer with the complete file exactly once, in a single ```html code block: no drafts, partial snippets or notes.
Keep planning brief and write the code. Aim for roughly 600 to 800 lines, compact and well structured.