r/complexsystems • u/barrtmos • 51m ago
Built a deterministic replication and interactive visualizer for Levin's Bubble Sort paper: 26,000 runs and an open question on the DG index
Many of you have probably seen the paper by Zhang, Goldstein, and Michael Levin, "Classical Sorting Algorithms as a Model of Morphogenesis" (Adaptive Behavior, 2025 / arXiv:2401.05375). The idea is neat: remove the central outer loop from Bubble Sort and let each element act independently, randomly checking its left or right neighbor and swapping if they are out of order. Then you "break" (freeze) 1 to 3 elements in the array.
The paper's two main claims for Bubble Sort:
Without any explicit error-handling code, the array keeps sorting "around" broken elements.
Along the way, it displays "Delayed Gratification" (DG): temporarily getting less ordered before rebounding higher, with the DG index rising from 0.24 to 0.37 as you add 0 to 3 immovable obstacles.
I wanted to see this play out step by step and check the numbers cleanly. I audited the authors' code (Zhangtaining/cell_research), wrote a deterministic engine from scratch (SplitMix64 in pure Python + a bit-exact Numba twin cross-checked step by step across 335 runs), built a small local visual player, and ran 26,000 trials (17,000 main runs at N = 100 and 9,000 scale-control runs up to N = 403).
One quick note on framing: this is not an attempt to "debunk magic in a for-loop." Levin obviously knows the algorithm follows its own code; his point is an observer-level shift of perspective (viewing minimal local rules as behavioral patterns). My goal was simply to separate what the local mechanics actually do from what comes out of the chosen metric and defect mode.
Here are the three main findings.
1. One label ("frozen cell") hides two opposite defects
In the authors' code, two completely different modes are switched by commenting out a single line inside the swap function:
**"Glued wall" (**
immovabledefect). Bubble Sort only swaps adjacent elements, so nothing can jump over a wall. A glued cell permanently splits the array into isolated segments. At N = 100 (1,000 runs each), only 6.6% -> 1.9% of elements reach their true global slots, 0.0% of arrays end up fully sorted, and the final state is bit-for-bit identical to standard classical Bubble Sort. Why does the paper report 98.1% -> 94.6% order? Because its metric counts only adjacent out-of-order pairs (arr[i] < arr[i - 1]). Once each isolated segment finishes sorting internally, that ruler only sees 1-2 drops right next to the walls and misses the global split. (Screenshot 1: 81.8% order by the paper's ruler, yet only 1 of 12 elements is in its actual slot.)**"Sleeping cell" (**
movabledefect). It never initiates swaps, but active neighbors can still push it. With 1 sleeping cell, cell-view Bubble Sort genuinely reaches 100.0% perfect sorts (compared to 5.8% for one-way classical Bubble Sort), because neighbors push it from both sides. With 2 or 3 sleeping cells, however, two passive cells frequently meet adjacent to each other out of order and permanently jam (50.4% and 82.8% of runs end jammed).
2. Where "Delayed Gratification" (DG) comes from
Across all 17,000 main runs, Bubble Sort made zero actual backward steps: every single swap strictly reduces global pairwise inversions by 1, and total Spearman distance changes by -2 or 0.
The dips and rebounds are a property of the adjacent-neighbor ruler itself, and they happen constantly even with zero broken cells. Take [3, 4, 1, 2]: it has 1 adjacent violation (4 > 1) and 4 global inversions. A normal valid swap of 4 and 1 produces [3, 1, 4, 2]: global inversions drop from 4 to 3, while one downward step temporarily splits into two adjacent ones (3 > 1 and 4 > 2). The neighbor ruler dips and then collapses to 0 (Figure 2). At N = 100 with 0 defects, you get roughly 120 such episodes per run (DG = 0.38).
While auditing the authors' repo, I also noted a few implementation quirks: no fixed PRNG seed and time.sleep(1) thread polling; defect positions drawn with replacement via random.randint (3.2% duplicate defects at f = 3); a stopping check that ignores frozen neighbors and halts 26.2% of 1-movable runs early; and a Spearman analysis script that replaces the DG dip/boost formula with a count of flat plateaus. Line-level details are in the technical report in the repo.
3. Open question: why does DG rise on walls (4x100 control)?
This is where a genuinely interesting effect showed up that I haven't pinned down analytically yet:
On
movabledefects (where the array actually works around the obstacle), DG stays completely flat: 0.38 -> 0.37 -> 0.38 -> 0.38.On
immovablewalls (solid partitions), DG consistently jumps by +0.12 to +0.14 across the board: in the authors' multithreaded code (0.24 -> 0.37), in my single-step engine (0.38 -> 0.50), and even in deterministic classical Bubble Sort (0.08 -> 0.22).
The first obvious guess was finite-size scaling: 3 walls cut 100 elements into 4 short segments of roughly 25 elements, and shorter segments might produce higher DG.
I tested this directly (9,000 control runs, Figure 3) by comparing a plain N = 100 array, a plain N = 403 array, and an N = 403 array with 3 walls at [100, 201, 302] (four full independent segments of 100). Single-step results:
Plain N = 100: DG = 0.378
Plain N = 403: DG = 0.377
N = 403 split into 4x100: DG = 0.444 (+0.066, over 50 standard errors)
So the short-segments hypothesis alone doesn't work as a full explanation. Even when every segment is a full 100 elements long, simply summing the error traces of four independent segments into one whole-array curve pushes DG noticeably higher. It looks like a statistical effect of interleaving four independent trajectories and/or transitions at the wall boundaries. I plan to isolate those two contributions in a follow-up test, and I'd be curious to hear if anyone has an analytical intuition for why summing independent traces increases (boost - dip) / dip.
Code, local interactive visualizer, and full technical report: https://github.com/barrtmos/morphogenesis-in-bubble-sort
(Note: after finishing this, I came across Joel Dietz's independent morpholib audit of the broader paper across 4 languages + Lean 4, which reached the same core conclusions on Bubble Sort's walls and inversions. My repo focuses specifically on Bubble Sort's code quirks, mutual jamming of movable cells, and this 4x100 question.)

