How the Networks Were Found

Methods and history of the 65 to 128 input median and sorting tables

This page explains how the networks in the tables were found and checked, and how the current answers came about. They are the best I have today, and I will update both pages as better networks turn up.

Contents: what the tables list, how every network is checked, sorting methods, median methods, how we got here, how the work was done, what comes next.

What the tables list

For every input count n from 65 to 128 the tables give the smallest comparator networks I could find and verify for two jobs: median selection (for odd n the median comes out on the middle wire; for even n the two middle values come out on the two middle wires) and full sorting. Each network has a size (the number of comparators, the logic cost) and a depth (the number of layers, the pipeline stages in an FPGA). A smaller network is often deeper, so the tables list every (size, depth) pair that no other listed network beats on both: today 139 median networks at 64 sizes and 175 sorting networks at 64 sizes.

For 64 inputs and fewer the reference is Bert Dobbelaere's tables of median networks and sorting networks. Most of those networks come from his program SorterHunter, which stores a test pattern in a single 64-bit word, which is likely why the published tables stop at 64 inputs. Their top row, n = 64, is my starting point: 521 comparators at depth 21 for sorting and 337 comparators at depth 23 for the median.

How every network is checked

Every network listed passed two independent checks: the search engine's own exhaustive check and a separately written bit-parallel verifier. A network that has not passed both is not listed, however good it looks.

Both checks rest on the 0-1 principle: a comparator network sorts every input if and only if it sorts every input made of zeros and ones. For the median of n = 2m + 1 inputs it is enough to test the critical inputs, those with exactly m or m + 1 ones: the middle wire must come out 0 for every input with m ones and 1 for every input with m + 1 ones, and the other inputs follow from that.

That is still 2n or close to it, far too many for 65 to 128 wires. What makes an exhaustive check possible is the prefix: the first layers of each network are a fixed structure whose possible outputs can be listed exactly, and only those outputs have to be pushed through the rest. Two kinds of prefix do most of the work here:

00000001000001110000111101111111row 0: wires 0 to 7row 3: wires 24 to 31
A 4 × 8 grid (32 inputs) after the grid prefix. Rows and columns are sorted, so the zeros always form a staircase from the top left; a 4 × b grid has only C(b + 4, 4) such states, and the rest of the network only has to sort those.

Sorting: the methods, in the order they arrived

The sorting methods came one after another, each starting from the networks the one before had left. In order:

Published sorters joined by Batcher merges (12 to 13 November)

The first table for every n from 65 to 128 was built, not searched: split the inputs into blocks of at most 64, sort each block with the published sorter, and join the sorted blocks with Batcher's odd-even merge, taking the cheapest tree. For 65 inputs, for example, a 33-sorter (199 comparators) and a 32-sorter (185) plus a merge (167) give 551. This is the starting table that everything below is measured against. On 20 November the published 64-input sorter was added as a block wherever that made the tree strictly smaller (n = 112, 115, 116, 117, 124, 127 and 128); the starting-table sizes quoted on this page are those of that table. These networks are still in the tables as the low-depth choice: at 64 of the 64 sizes the shallowest sorter listed is one of them.

Grid sorters (19 to 21 November)

A grid prefix (above) followed by a searched tail. The tail search is a random walk in the style of SorterHunter: change the tail by one small move (remove a comparator, swap two, cross two over, and so on), keep the change if the network still sorts every state of the grid and is no larger. A first pilot on 19 November stalled. With 10 to 100 times the compute it worked: on 20 November a 72-input sorter of 642 comparators beat the starting table's 643, followed by 589 at 68, 638 at 72 and 694 at 76. Progress came in rounds of 8 to 24 seeds, each restarted from the best tail of the round before; the 76-input tail went from 291 to 259 comparators over six rounds, about 600 CPU-hours.

Removing inputs (20 to 21 November)

Fix one input of a sorter to minus or plus infinity: every comparator that value meets does nothing and can be deleted, which leaves a sorter for one input fewer. So 589 at 68 gave 581 at 67.

Column doubling (23 November)

The idea that changed the most came from an independent review: in a 4 × b grid the even columns form a 4 × b/2 grid, and so do the odd columns. Sort both halves with the best half-width tail, then two layers of comparators between the halves finish the sort (4b − 3 comparators before pruning). Such a starting network takes minutes to build. At 88 inputs it gave 853, against 863 in the starting table, where the plain walks had stood at 868 after about 1,000 CPU-hours. From then on every grid search at 84 inputs and up started from a column-doubled network, so the grid-sorter rows from 84 up in the tables owe their start to doubling.

even columnsodd columnseach colour is a 4 × 4 grid,sorted by the best 16-input tail;two more layers merge the halves
Column doubling on 32 inputs (4 × 8). Each colour is a 4 × 4 grid of 16 inputs, sorted by the best 16-input tail at the same time; two more layers finish the sort.

Input removal with the layers kept (23 November, night)

Removing an input works with the grid networks too, but the removal tool had been rearranging the layers, which broke the grid structure the checkers rely on: the sets of states they had to enumerate grew from about a hundred to 1012 to 1015. Keeping every comparator in its original layer let both checkers carry the state set through every layer again, ending with only the n + 1 sorted outputs, so every gain at a multiple of four could give up to three more sizes.

Ragged grids (24 November)

Doubling needs both halves to be grids, so at first it only worked at multiples of four. A ragged grid fixes that: for n = 4h + j use four rows of length h and make the first j rows one cell longer. Its even and odd columns are again ragged grids, so doubling works at every n. At 04:41 on 24 November 45 sizes passed both checkers at once.

Small shapes compounding upward (from 24 November)

A doubled network contains two copies of the half-size one, and doubling again gives four. So one comparator saved on a small shape is saved twice at twice the size and four times at four times the size. From 24 November spare threads ran long searches on the small ragged shapes, and every gain was carried up by rebuilding the larger networks from it.

Staircase grids and drops from walked tails (24 to 25 November)

A staircase grid lets the four rows have different lengths, so a row can take a length whose published sorter is cheap (16, 18, 20, 24, 32) while another row takes the rest. For 69 inputs, rows of 21, 16, 16 and 16 give a 359-comparator prefix and a 243-comparator tail: 602, five below the size the round started from. The first 13 staircase networks passed both checkers at 22:20 on 24 November; with them every n from 65 to 128 was below the starting table. Most winning staircase tails were not searched at all: they are the tail of a larger, heavily searched grid with its last cells fixed to plus infinity (a drop). A pass over all n takes about nine minutes on one thread.

ragged grid, 22 inputs: rows 6, 6, 5, 5staircase grid, 69 inputs: rows 21, 16, 16, 16
Two grid shapes. Ragged (shaded by column parity): the even columns form a 4 × 3 grid, the odd columns rows of 3, 3, 2, 2, another ragged grid. Staircase: one long row and three rows of 16.

Today 64 of the 64 sizes are below the starting table, by 481 comparators in all. At 65 inputs the smallest sorter is 543 (starting table 551), at 128 it is 1425 at depth 36 (starting table 1427).

What did not work

Median: the methods

My 2024 layer-by-layer greedy

In February 2024 I built an 81-input median network one layer at a time. After a hypercube prefix (five blocks of 16 wires plus one free wire, 4 layers, 160 comparators), every critical input is pushed through the network so far and duplicates are merged; the unresolved states are those whose middle wire is not yet settled. Each new layer is chosen in two stages: first wire clearing, comparators after which a wire holds a value known to be below (or above) the median in every remaining state, so that wire leaves the problem for good; then fill, the remaining pairs that resolve the most states. In 2024 a few states left at the very end were closed by hand-placed comparators, and at three points the last pairs of a layer were dropped by hand. The result was 498 comparators at depth 28. It passed both checkers on 25 November 2026, and it is still the smallest 81-input median in my table for every depth from 28 to 33. (Depth here counts layers after packing every comparator into the earliest layer it fits, as in the tables; that is why my GitHub page gives this network depth 30 and this page 28.)

Hypercube prefixes

A hypercube on a block of 2k wires is k layers of comparators with strides 1, 2, 4, and so on. It is cheap, and it shrinks the set of states the rest of the network has to handle: after a full 64-wire hypercube (6 layers, 192 comparators) only 7,828,354 distinct outputs remain instead of 264. The median searches use combinations of hypercube blocks (for example 64 + 16 + 1 wires for n = 81), usually mirrored: a network that is symmetric under reversing the wires and swapping 0 and 1 only needs half of the critical inputs checked.

01234567stride 1stride 2stride 4
The 8-wire hypercube: three layers with strides 1, 2 and 4, twelve comparators.

Greedy with a SAT finisher

The search engine has a port of the 2024 rule. It builds layers until progress stalls and then hands the last few layers to a SAT solver, which finishes the network exactly. This gave the starting networks of the median campaign; 9 rows in the table today come straight from it.

Mutation search ("evolve")

SorterHunter's method carried past 64 wires: keep the prefix, mutate the rest one small move at a time, and keep a mutant when it is still a correct median network and no larger. To make each test fast, the first part of the network is often held fixed as well (a cut) and only the states left at the cut are tested. This search holds the smallest network at 46 of the 64 sizes. At 81 inputs the smallest listed network is 479 comparators at depth 38, from evolve.

Networks inherited from larger ones

As with sorting, fixing one input of a correct median network to plus or minus infinity (one of each for two inputs fewer, to keep the median in the middle) and deleting the comparators it meets gives a correct network for fewer inputs. Even sizes can also be built from the odd size above. This gives the smallest network at 14 sizes today, and redundancy removal (proving that some comparators of a finished network are never needed and deleting them) at 4.

The endgame

Hold all but the last few layers of a finished network fixed and ask a SAT solver for the smallest correct replacement of the tail. In November it took an earlier reconstruction of my 2024 network from 505 comparators at depth 31 to 500 at depth 28. The searches above have since gone further, and none of the rows in the table today come from it directly.

What did not work

How we got here

The short version, with the dates of the step changes:

Sorting: where today's rows come from

MethodRows in the tableSizes where it gives the smallest network
Published sorters joined by Batcher merges (the starting table)640
4-row grid, searched tail (from 23 November, at 84 inputs and up, started from column-doubled networks)2613
4-row grid network with 1 to 3 inputs removed70
4-row grid network with 1 to 3 inputs removed, layers kept71
Ragged grid, column doubling4628
Staircase grid, column doubling2522

Median: where today's rows come from

MethodRows in the tableSizes where it gives the smallest network
My 2024 layer-by-layer greedy10
Greedy construction with a SAT finisher90
Mutation search (evolve)10646
Inherited from a larger network1914
Redundant comparators removed44
Endgame: tail re-solved on a fixed prefix00

Counted from the verified result store when this page was generated. A row is one (size, depth) pair in the tables.

How the work was done

The searches ran on my home lab machines. They were directed day to day by AI coding agents (Claude), which wrote and ran the search code under my direction.

What comes next

The tables will be updated as better networks are verified; the generated date at the bottom of each page says when they were last rebuilt.

On the median side the next step is a multi-layer lookahead version of my 2024 greedy: instead of choosing one layer at a time, search two layers ahead as a tree of choices, keep the first, and repeat (three or more layers later, if it pays and is affordable). Since no simple score predicted the cost of the rest of the network, the plan is to first finish many candidates completely, use those runs to fit a score, and then search with the fitted score. The first target is an 81-input median smaller than 498 from the same kind of construction, then other sizes from 65 to 127.

Generated 2026-11-25 from the verified result store. Dates are the campaign's, in MST; back to the tables.