Four sorts, one starting line
C# records every array write four classic sorts make on the same shuffled input, and an HTML cell replays the recordings side by side at equal speed. O(n log n) wins, visibly.
Four sorts, one starting line
Big-O notation is easier to feel than to read. The C# cell below runs four classic sorting algorithms over the same shuffled array and records every single array write each one makes; the HTML cell then replays those recordings side by side at identical speed, one write at a time. Watch quicksort and mergesort lap the quadratic algorithms, then check the tally under the code.
1using System.Text.Json;23var rng = new Random(20260704);4var initial = Enumerable.Range(1, 64).OrderBy(_ => rng.Next()).ToArray();56var races = new List<object>();7var tallies = new List<object>();89int[] a = Array.Empty<int>();10var ops = new List<int[]>();11var comparisons = 0;1213void Write(int i, int v) { a[i] = v; ops.Add(new[] { i, v }); }14bool Less(int x, int y) { comparisons++; return x < y; }15void Swap(int i, int j) { if (i == j) return; var t = a[i]; Write(i, a[j]); Write(j, t); }1617void Bubble()18{19 for (var pass = a.Length - 1; pass > 0; pass--)20 {21 var swapped = false;22 for (var j = 0; j < pass; j++)23 if (Less(a[j + 1], a[j])) { Swap(j, j + 1); swapped = true; }24 if (!swapped) return;25 }26}2728void Insertion()29{30 for (var i = 1; i < a.Length; i++)31 {32 var key = a[i];33 var j = i - 1;34 while (j >= 0 && Less(key, a[j])) { Write(j + 1, a[j]); j--; }35 Write(j + 1, key);36 }37}3839void Quick(int lo, int hi)40{41 if (lo >= hi) return;42 var pivot = a[hi];43 var i = lo;44 for (var j = lo; j < hi; j++)45 if (Less(a[j], pivot)) Swap(i++, j);46 Swap(i, hi);47 Quick(lo, i - 1);48 Quick(i + 1, hi);49}5051void MergeSort(int lo, int hi)52{53 if (lo >= hi) return;54 var mid = (lo + hi) / 2;55 MergeSort(lo, mid);56 MergeSort(mid + 1, hi);57 var merged = new List<int>(hi - lo + 1);58 int left = lo, right = mid + 1;59 while (left <= mid && right <= hi)60 merged.Add(Less(a[right], a[left]) ? a[right++] : a[left++]);61 while (left <= mid) merged.Add(a[left++]);62 while (right <= hi) merged.Add(a[right++]);63 for (var k = 0; k < merged.Count; k++) Write(lo + k, merged[k]);64}6566void Race(string name, Action sort)67{68 a = (int[])initial.Clone();69 ops = new List<int[]>();70 comparisons = 0;71 sort();72 races.Add(new { name, ops });73 tallies.Add(new { Algorithm = name, Comparisons = comparisons, Writes = ops.Count });74}7576Race("Bubble sort", Bubble);77Race("Insertion sort", Insertion);78Race("Quicksort", () => Quick(0, a.Length - 1));79Race("Merge sort", () => MergeSort(0, a.Length - 1));8081var sortRace = JsonSerializer.Serialize(new { start = initial, races });8283// Returning the collection hands it to Verso's formatter, which renders a table.84tallies
List<Object> (4 items)
| Algorithm | Comparisons | Writes |
|---|---|---|
| Bubble sort | 1863 | 2170 |
| Insertion sort | 1144 | 1148 |
| Quicksort | 317 | 378 |
| Merge sort | 302 | 384 |
1<div style="font-family:var(--verso-prose-font-family,sans-serif);">2 <div style="display:flex;align-items:center;gap:10px;flex-wrap:wrap;margin-bottom:10px;">3 <button id="sr-replay" type="button" style="padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;">Replay race</button>4 <label style="font-size:12.5px;color:var(--verso-fg-muted,#777);">speed5 <select id="sr-speed"6 style="font:inherit;font-size:12.5px;color:inherit;background:transparent;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:6px;padding:2px 4px;">7 <option value="1" selected>1x</option>8 <option value="2">2x</option>9 <option value="4">4x</option>10 </select>11 </label>12 <span style="margin-left:auto;font-size:12.5px;color:var(--verso-fg-muted,#777);">every lane replays at the same writes-per-second</span>13 </div>14 <div id="sr-lanes" style="display:flex;gap:14px;flex-wrap:wrap;"></div>15</div>16<script>17(function () {18 var data = @sortRace;19 var n = data.start.length;20 var lanesBox = document.getElementById('sr-lanes');21 var speedSel = document.getElementById('sr-speed');22 var finished = 0;23 var ORDINALS = ['1st', '2nd', '3rd', '4th'];2425 var lanes = data.races.map(function (race) {26 var wrap = document.createElement('div');27 wrap.style.cssText = 'flex:1 1 180px;min-width:170px;max-width:250px;';28 var head = document.createElement('div');29 head.style.cssText = 'display:flex;justify-content:space-between;align-items:baseline;'30 + 'font-size:12.5px;margin-bottom:4px;';31 var name = document.createElement('span');32 name.textContent = race.name;33 name.style.fontWeight = '500';34 var badge = document.createElement('span');35 head.appendChild(name);36 head.appendChild(badge);37 var canvas = document.createElement('canvas');38 canvas.width = 220;39 canvas.height = 130;40 canvas.style.cssText = 'width:100%;display:block;border-radius:6px;';41 var count = document.createElement('div');42 count.style.cssText = 'font-size:11.5px;margin-top:3px;font-variant-numeric:tabular-nums;'43 + 'color:var(--verso-fg-muted,#777);';44 wrap.appendChild(head);45 wrap.appendChild(canvas);46 wrap.appendChild(count);47 lanesBox.appendChild(wrap);48 return { race: race, canvas: canvas, ctx: canvas.getContext('2d'), badge: badge,49 count: count, arr: data.start.slice(), pos: 0, hot: -1, done: false };50 });5152 function drawLane(lane) {53 var ctx = lane.ctx, w = lane.canvas.width, h = lane.canvas.height;54 ctx.fillStyle = '#10131a';55 ctx.fillRect(0, 0, w, h);56 var bw = w / n;57 for (var i = 0; i < n; i++) {58 var v = lane.arr[i];59 ctx.fillStyle = lane.done ? 'hsl(140, 55%, 52%)'60 : i === lane.hot ? '#ffffff'61 : 'hsl(' + Math.round(200 + 140 * (v - 1) / (n - 1)) + ', 80%, 60%)';62 var bh = (h - 8) * v / n;63 ctx.fillRect(i * bw + 0.5, h - bh, bw - 1, bh);64 }65 lane.count.textContent = lane.pos.toLocaleString() + ' / '66 + lane.race.ops.length.toLocaleString() + ' writes';67 }6869 function reset() {70 finished = 0;71 lanes.forEach(function (lane) {72 lane.arr = data.start.slice();73 lane.pos = 0; lane.hot = -1; lane.done = false;74 lane.badge.textContent = '';75 drawLane(lane);76 });77 }7879 function tick() {80 var budget = 6 * Number(speedSel.value);81 lanes.forEach(function (lane) {82 if (lane.done) return;83 var ops = lane.race.ops;84 for (var s = 0; s < budget && lane.pos < ops.length; s++) {85 var op = ops[lane.pos++];86 lane.arr[op[0]] = op[1];87 lane.hot = op[0];88 }89 if (lane.pos >= ops.length) {90 lane.done = true;91 lane.hot = -1;92 lane.badge.textContent = ORDINALS[finished++];93 lane.badge.style.cssText = 'color:hsl(140, 55%, 48%);font-weight:600;';94 }95 drawLane(lane);96 });97 }9899 function loop() {100 if (!lanesBox.isConnected) return;101 if (lanes.some(function (l) { return !l.done; })) tick();102 requestAnimationFrame(loop);103 }104105 document.getElementById('sr-replay').addEventListener('click', reset);106 reset();107 requestAnimationFrame(loop);108})();109</script>
The tally tells the same story
Same input, same rules, wildly different amounts of work: the table under the C# cell counts every comparison and every array write each algorithm performed. The quadratic sorts made thousands of writes where mergesort needed a few hundred, and that gap only widens as the array grows; at a million elements it is the difference between a blink and a coffee break.
The replay is honest, too. Every lane advances at the same writes-per-second, so the finishing order you watched is exactly the story the numbers tell.
Run it yourself
Nothing on this page is executable; the outputs were baked in by a real run. Download the notebook and it runs anywhere Verso does: VS Code, the browser, or the terminal.
dotnet tool install --global Verso.Cliverso serve sorting-races.versoWhat this shows
C# records every array write four classic sorts make on the same shuffled input, and an HTML cell replays the recordings side by side at equal speed. O(n log n) wins, visibly.
Every language in this notebook reads and writes the same variable store, so a value produced in one cell is simply there in the next, whatever the language.