13. Unary Array Sort
The river Emajõgi was breaking its winter shell, each crack in the ice echoing like distant musket fire through the streets of Dorpat. Meltwater gurgled along the cobbles, carrying the grit of a long season toward the bridges.
In the back room of the printing house, the air was warmer, thick with ink and steam from a dented samovar. Stacks of thin paper leaned precariously against the wall, each sheet marked with a single poem—verses Liina had gathered from farmers, seamstresses, and wandering fiddlers.
She stood over the table, brow furrowed, shuffling the pages again and again. „I want them in order,“ she said without looking up. „Shortest to longest. The eye should climb the page like a hill—soft at first, then steep.“
Johann lounged on a crate of type, peeling an apple with his penknife. „They’re poems, not firewood. Why measure them?“
Liina shot him a glance sharp enough to nick the blade. „Because arrangement is part of meaning.“
Mihkel had been silent, running his fingers over the edge of one sheet. The ink bled just slightly into the fibres—old, careful handwriting. „How many are there?“ he asked.
„Thirty-four.“
„And you want them sorted by line count?“
She nodded. „I could do it by eye, but… it is slow. And I cannot trust my memory when they’re all so different.“
Johann snorted. „What’s next, measuring rhyme with calipers?“
Mihkel ignored him. His mind was already elsewhere—not on verses, but on processes. This was not addition, or even comparison between two numbers. This was arranging many, one against another, until all found their place.
That night, in the hush of his workshop, he copied each poem’s length into unary, separated by commas, onto a long strip of tape. The Mill had compared before—but now it would compare, swap, and repeat, until no pair was out of order.
As the crank turned, the sound was different—more iterative, a rhythm of trial and correction. The Mill was not rushing to an answer; it was circling, refining, drawing closer with each pass.
He watched it work with a quiet satisfaction. This was no trick—it was method made flesh in brass and steel.
On the input tape, you’ll get one or more positive numbers in the unary format separated by commas. Your task is to sort these numbers in ascending order.
For example, if the input tape is
||,|,|||||,||||||||
,
your output tape should be
|,||,|||||,||||||||
.