What the libraries do

The rotation that could be priced

A sort that finds a sorted file with a late tail can send the tail's pieces straight into the head or merge them to one group first, and a rule that priced both ends as shifts chose wrongly at a buffer of a 128th, because a group longer than the buffer crosses the head by rotation. Priced instead from measurements of the library merge on synthetic runs, the long crossing comes within 3% of what the sort pays at every buffer and the choice at a 128th comes right. But the rule prices the pieces against the head as it stood before any of them joined it, and each piece also crosses the pieces before it: priced up to 37% low, they send the sort to the dearer end at a 256th.

How many pieces the head can afford sorted a file that arrives almost in order: 65,536 keys, sorted except for a last tenth appended in random order from the top fifth of the keys. A stable sort with a buffer of bb keys can sort that tail a buffer’s worth at a time and then either send each sorted piece straight into the head, or merge the pieces among themselves to one group and cross the head once. Pieces that fit the buffer beat the library’s merge sort down to a buffer of a 128th, and below that the cheapest thing was to merge them all the way first. Merging partway, which the essay had proposed, was never the cheapest; the choice was between the two ends.

A rule that compared the two ends had priced both as shifts. Sending pieces in costs each piece’s size plus the head keys above its smallest, the moves a merge into the head makes when nothing rotates. Merging first costs two moves a tail key a round plus one crossing of the whole tail, priced the same way. That rule chose correctly everywhere but at a 128th, where it merged first and paid 3.55 moves a key when sending the pieces in cost 3.20. Its closing section found why: a group longer than the buffer crosses the head by the library’s rotation-based split, and rotation moves far more keys than shifting does. It proposed pricing that rotation from measurements of the library merge itself, a long run crossing a longer one at every buffer, and predicted that the priced rule would choose the cheaper end at every buffer and every reach of the tail measured, including the 128th. It named the risk that where the two ends are close, at a 64th, the price’s error might exceed their difference.

The rotation can be priced, and closely. The risk landed somewhere else.

Prices measured on runs the sort never sees

The input, the buffers and the sort are the earlier essay’s, and every move is counted by the library merge’s own counter, so a move in a price is the same move the sort would make. The rule has two prices, both taken from runs of the library merge on synthetic inputs built for the purpose, never on the file being sorted.

The first is a crossing: one sorted run of tt keys merged into a sorted head of n−tn - t keys, when the run’s keys are spread at random among the head’s top AA keys and its own, at buffer bb. It is measured on a doubling grid of AA from an eighth of tt to 64 times tt, at each run length and buffer the rule needs, and read between grid points by interpolating in log⁡A\log A. The second is the rounds: the moves of merging ⌈t/b⌉\lceil t/b \rceil sorted pieces of bb random keys all the way to one group, at buffer bb.

Before moving anything but the pieces’ own sorting, which both ends share, the rule reads each piece’s AA by one binary search of its smallest key in the head, the search a gallop from the end of a merge makes. It prices sending the pieces in as the sum of their crossings, and merging first as the rounds plus one crossing of the whole tail at the AA of the tail’s smallest key, and it takes the cheaper. Every calibration merge is checked sorted, and the sort’s own result sorted and stable.

The long crossing priced to within three per cent

Measured on synthetic runs, the price of the tail's one crossing of the head is within 3% of what the sort paid at every buffer, 2.23 moves a key against 2.19 at a 1,024th, where shifts price it at 0.20; the rounds before it are priced within 2%, where two moves a key a round says 1.40 against 3.62At each buffer, for the tail merged to one group first: the moves a key of the file the final crossing of the head cost, its price from the measured table, and its price as shifts (the head keys above the tail's smallest plus the tail); and the rounds inside the tail, paid, priced from a measured merge of random pieces, and priced at two moves a tail key a round. 1/16: crossing 0.449, 0.450, 0.200; rounds 0.137, 0.137, 0.200; 1/32: crossing 0.746, 0.750, 0.200; rounds 0.344, 0.345, 0.400; 1/64: crossing 1.045, 1.047, 0.200; rounds 0.682, 0.681, 0.600; 1/128: crossing 1.305, 1.340, 0.200; rounds 1.145, 1.168, 0.800; 1/256: crossing 1.605, 1.642, 0.200; rounds 1.802, 1.791, 1.000; 1/1024: crossing 2.186, 2.225, 0.200; rounds 3.624, 3.583, 1.400; 1/4096: crossing 2.693, 2.720, 0.200; rounds 5.803, 5.757, 1.800.paidpriced from measurementspriced as shiftsupper three bars: the crossing · lower three: the rounds0123451/161/321/641/1281/2561/10241/4096moves a key of the filethe tail merged to one firstbuffer, as a share of the file
Fig. 1 At each buffer, for the tail merged to one group first: the final crossing of the head paid, priced from measurements, and priced as shifts, in moves a key of the file; and the rounds inside the tail, paid, priced, and priced at two moves a tail key a round. At a 1,024th: crossing 2.19, 2.23 and 0.20; rounds 3.62, 3.58 and 1.40. At a 16th: crossing 0.45, 0.45 and 0.20; rounds 0.137, 0.137 and 0.20.

The measured price of the tail’s one crossing of the head comes within 3% of what the sort pays at every buffer: 2.23 moves a key of the file against 2.19 at a 1,024th, where pricing it as shifts says 0.20. The rounds before it are priced within 2%, 3.58 against 3.62 at a 1,024th, where two moves a tail key a round says 1.40. Shifts underpriced both, and by a factor that grows as the buffer shrinks: the crossing by a factor of eleven at a 1,024th and of two at a 16th.

The shifts price is the same at every buffer, 0.20 moves a key, because it counts each key the crossing passes once: the tail’s 6,554 keys and the 6,554 or so head keys above the tail’s smallest. The library merge does that only when the shorter side of a merge fits the buffer. When it does not, the merge splits the longer side, rotates the pieces into place, and recurses, and every rotation moves keys that a shifting merge would have left alone. The moves no stable sort can skip counted those rotations as the price of sorting stably in little space. A smaller buffer means more levels of splitting and more rotation, which is why the paid crossing rises from 0.45 to 2.69 moves a key from a 16th to a 4,096th while the shifts price stays flat.

One run of 6,554 keys crossing a head it interleaves with costs 6.76 moves a key of the run with a buffer of 4,096 and 38.36 with a buffer of 64, at 16,384 head keys above its smallest: the rotation a small buffer forces is the part of the price that shifts cannot see, and it is measured here once per bufferThe measured moves, per key of the run, of merging one sorted run of 6,554 keys into a sorted head of 58,982 when its keys are spread at random over the head's top A keys and its own, at buffers of 4096, 1024, 256, 64. buffer 4096: A 1,024 1.31, A 2,048 1.62, A 4,096 2.25, A 8,192 4.85, A 16,384 6.76, A 32,768 10.47; buffer 1024: A 1,024 1.31, A 2,048 4.02, A 4,096 7.66, A 8,192 11.58, A 16,384 17.19, A 32,768 28.50; buffer 256: A 1,024 4.41, A 2,048 7.77, A 4,096 11.67, A 8,192 18.35, A 16,384 27.70, A 32,768 46.37; buffer 64: A 1,024 8.15, A 2,048 11.26, A 4,096 16.58, A 8,192 25.12, A 16,384 38.36, A 32,768 64.64. Both axes logarithmic.head keys above the run's smallest (log scale)moves a key of the run (log scale)12481632641,0244,09616,384buffer of 4,096buffer of 1,024buffer of 256buffer of 64a run of 6,554 keys, a tenth of the filemeasured on synthetic runs
Fig. 2 The measured price table: one run of 6,554 keys crossing a head of 58,982 that it interleaves with, moves a key of the run against the head keys above its smallest, at four buffers. At 16,384 head keys above: 6.76 with a buffer of 4,096, 17.2 with 1,024, 27.7 with 256, 38.4 with 64. At 1,024 above: 1.31, 1.31, 4.41 and 8.15.

A run of a tenth of the file crossing 16,384 head keys costs 6.76 moves a key of the run with a buffer of 4,096 and 38.4 with a buffer of 64. That is the table the rule reads. Each curve rises with the head keys the run interleaves with, as shifts would, and the curves separate by buffer, which shifts cannot: a run that the buffer cannot hold pays for every split it forces, and the number of splits grows as the buffer shrinks. Where the run interleaves with few head keys, 1,024 of them, a large buffer merges it at about a move and a third a key, which is shifting; a buffer of 64 already pays eight.

The table is measured on runs whose keys are spread at random over their range, the property of an input its runs describe, and the sort’s tail is exactly that, so the prices match closely. They match because the library merge’s work depends on how the two runs interleave and on the buffer, not on which keys they hold, and that is the condition under which a measurement on one input prices another.

The choice at a 128th comes right

Priced from measurements of the library merge, the tail merged to one group is priced within 2% at every buffer, which puts the choice right at a 128th, where pricing by shifts had it wrong; but the pieces sent straight in are priced low by up to 37%, and at 1/256 that sends the sort to the dearer endMoves a key of a 65,536-key file whose last tenth arrives in random order from the top fifth of the keys, at each buffer: what each end paid and what the rule priced it at before moving anything, the pieces' own sorting included in both. 1/16: straight in paid 1.98, priced 1.93; merged first paid 2.10, priced 2.10; chose straight in, the cheaper. 1/32: straight in paid 2.16, priced 1.99; merged first paid 2.47, priced 2.47; chose straight in, the cheaper. 1/64: straight in paid 2.46, priced 2.17; merged first paid 2.96, priced 2.96; chose straight in, the cheaper. 1/128: straight in paid 3.20, priced 2.65; merged first paid 3.55, priced 3.60; chose straight in, the cheaper. 1/256: straight in paid 5.00, priced 3.86; merged first paid 4.35, priced 4.38; chose straight in, the dearer. 1/1024: straight in paid 16.04, priced 11.00; merged first paid 6.46, priced 6.46; chose merged first, the cheaper. 1/4096: straight in paid 58.46, priced 36.76; merged first paid 8.87, priced 8.85; chose merged first, the cheaper. The cheaper at 1/128 is straight in.buffer, as a share of the filemoves a key of the file (log scale)12481632641/161/321/641/1281/2561/10241/4096wrong endpieces straight in, paidpieces straight in, pricedmerged to one first, paidmerged to one first, priced65,536 keys, a late tenth from the top fifthdashed: priced in advance
Fig. 3 Moves a key of the file at each buffer, for each end paid (solid) and priced in advance (dashed), the pieces’ own sorting included. Sent straight in: 1.98, 2.46, 3.20, 5.00 and 16.0 paid at a 16th, 64th, 128th, 256th and 1,024th; priced 1.93, 2.17, 2.65, 3.86 and 11.0. Merged first: 2.10, 2.96, 3.55, 4.35 and 6.46 paid; priced 2.10, 2.96, 3.60, 4.38 and 6.46.

At a 128th the priced rule sends the pieces straight in, at 3.20 moves a key, where the shifts rule merged first and paid 3.55. The prediction’s named case comes right, and so does the case it named as a risk: at a 64th the two ends differ by half a move a key, 2.46 against 2.96, and the priced rule chooses the cheaper. Of the seven buffers it chooses the cheaper end at six.

The seventh is a 256th. There the pieces sent straight in cost 5.00 moves a key and the tail merged first 4.35; the rule prices them at 3.86 and 4.38 and sends the pieces in. Its price for merging first is right to within three hundredths of a move. Its price for sending the pieces in is a quarter too low.

The shifts rule was right at a 256th, and for no good reason. It priced sending the pieces in at 2.69 moves a key and merging first at 1.20, leaving out the pieces’ own sorting, and both were far below what the sort paid; the two errors were of different sizes, and here they happened to point the way that made the choice right. A rule whose errors are large can be right by luck at one buffer and wrong at the next, which is what it was at a 128th. The priced rule’s error is confined to one of its two prices, and it is wrong exactly where that price’s error exceeds the gap between the ends. The prediction expected the risk to lie in the crossing it could not price; the error is in the crossings it thought it could.

What the pieces cross

The pieces sent straight in are priced against the head as it was before any of them joined it, and each one after the first also crosses the pieces already merged: the price is 89% of what they cost at a 16th, 74% at a 128th and 67% at a 1,024th, where 103 pieces cross a head they keep enlargingAt each buffer, the moves a key the pieces' crossings of the head cost when sent straight in, against what the rule priced them at from the measured table, each piece's head keys above its smallest read before any piece is merged. 1/16: paid 0.462, priced 0.411, 89%; 1/32: paid 0.787, priced 0.614, 78%; 1/64: paid 1.227, priced 0.928, 76%; 1/128: paid 2.105, priced 1.553, 74%; 1/256: paid 4.051, priced 2.912, 72%; 1/1024: paid 15.385, priced 10.342, 67%; 1/4096: paid 58.088, priced 36.386, 63%.00.2500.5000.7501buffer, as a share of the filethe pieces' crossings: priced over paid1/161/321/641/1281/2561/10241/4096priced over paidone: priced exactlypieces straight into the head65,536 keys
Fig. 4 The pieces sent straight in: the price of their crossings over what the crossings cost, at each buffer. 89% at a 16th, 78% at a 32nd, 76% at a 64th, 74% at a 128th, 72% at a 256th, 67% at a 1,024th, 63% at a 4,096th.

The pieces’ crossings are priced at 89% of what they cost at a 16th, 74% at a 128th and 67% at a 1,024th: the price falls further behind the cost the more pieces there are. Each piece is priced against the head as it stood before any piece joined it. But the pieces go in one after another, and the head a piece meets holds every piece merged before it. Those pieces are drawn from the same top fifth of the keys, so a piece crosses not only the original head’s keys above its smallest but also most of the keys of the pieces already merged, and that second count grows with the number of pieces in front of it.

At a 16th there are two pieces and the second crosses one piece’s worth of extra keys; the price is short by a tenth. At a 1,024th there are 103 pieces and the last of them crosses the keys of a hundred others; the price is short by a third. The crossing table is right about what one crossing costs given its AA. The rule fed it the wrong AA, the one it could read off the head before moving anything, and every AA after the first is larger by the keys of the pieces already in.

As the tail reaches further

As the late tenth reaches further into the keys, the priced rule chooses the cheaper end at 9 of 10 settings and the shifts rule at 8; the priced rule's miss is a reach of 0.1 at 1/1024, where the pieces cross mostly each otherAt buffers of a 1,024th and a 128th and reaches of 0.1, 0.2, 0.4, 0.7, 1: the moves a key of each end, and the end each rule chose. 1/1024, reach 0.1: straight in 5.90, merged first 4.33; priced rule chose straight in (wrong), shifts rule straight in (wrong); 1/1024, reach 0.2: straight in 16.04, merged first 6.46; priced rule chose merged first (right), shifts rule merged first (right); 1/1024, reach 0.4: straight in 36.34, merged first 8.58; priced rule chose merged first (right), shifts rule merged first (right); 1/1024, reach 0.7: straight in 66.64, merged first 11.77; priced rule chose merged first (right), shifts rule merged first (right); 1/1024, reach 1: straight in 97.19, merged first 14.95; priced rule chose merged first (right), shifts rule merged first (right); 1/128, reach 0.1: straight in 1.89, merged first 2.33; priced rule chose straight in (right), shifts rule straight in (right); 1/128, reach 0.2: straight in 3.20, merged first 3.55; priced rule chose straight in (right), shifts rule merged first (wrong); 1/128, reach 0.4: straight in 5.79, merged first 4.82; priced rule chose merged first (right), shifts rule merged first (right); 1/128, reach 0.7: straight in 9.69, merged first 6.62; priced rule chose merged first (right), shifts rule merged first (right); 1/128, reach 1: straight in 13.58, merged first 8.42; priced rule chose merged first (right), shifts rule merged first (right).reach 0.1reach 0.2reach 0.4reach 0.7reach 1each cell: the end chosen, filled when it was the cheaper1/1024, priced rulestraightmergedmergedmergedmerged1/1024, shifts rulestraightmergedmergedmergedmerged1/128, priced rulestraightstraightmergedmergedmerged1/128, shifts rulestraightmergedmergedmergedmerged65,536 keys, a late tenthunfilled: the dearer end
Fig. 5 At buffers of a 1,024th and a 128th, with the late tenth drawn from the top tenth of the keys up to all of them: the end each rule chose, filled where it was the cheaper. The priced rule chooses the cheaper end at nine of ten settings, missing only at a 1,024th with the tail from the top tenth, where pieces sent straight in cost 5.90 moves a key and merged first 4.33. The shifts rule is right at eight.

As the late tenth is drawn from the top tenth of the keys up to all of them, the priced rule chooses the cheaper end at nine of the ten settings at a 1,024th and a 128th, and the shifts rule at eight. The one setting the priced rule misses is the one where the pieces cross the original head least: a tail of a tenth of the file drawn from the top tenth of the keys is the whole of that tenth, so the original head has no keys above any piece, and at a 1,024th every one of the 103 pieces crosses only the pieces merged before it. Priced against the original head alone, sending them in looks almost free, 0.76 moves a key of the file with the pieces’ own sorting included; it costs 5.90.

The further the tail reaches, the more of each piece’s crossing is of original head keys, and the closer the price comes. With the tail from the whole range, the pieces cost 97 moves a key at a 1,024th and are priced at 93; nothing about that choice is close, and both rules make it.

What the rotation was, and what it was not

The earlier essay’s closing section treated the rotation as the part of a merge a sort could not price without doing it, as a request that reads the input first had priced buffers from the runs it found rather than from the moves they would cost. Measured once per buffer on synthetic runs, it can: the crossing of a long group, which is almost all rotation at small buffers, comes within 3% at every buffer, and the rounds within 2%. The library merge’s cost depends on the buffer, on the run lengths and on how the runs interleave, and a table indexed by those three numbers carries it from one input to another. Pieces that fit the buffer began this strand by noticing that a merge that fits the buffer costs about shifts and one that does not costs much more; the table is that observation made exact.

What the rule could not price is the sequence. Sending the pieces in is not seven or a hundred independent crossings of one head; it is a hundred crossings of a head that each crossing enlarges. The rule’s price treated them as independent, and its error grew with how many there were. That is the error that made it wrong at a 256th and at the shortest reach, and it is an error in what was counted, not in the price of any one merge.

What the prediction got right

The closing section predicted that pricing the rotation from measurements of the library merge would let the rule choose the cheaper end at every buffer and every reach measured, including the 128th where the shifts rule chose wrongly, and so cost the cheaper end everywhere. It chooses correctly at the 128th, at six of seven buffers and at nine of ten reach settings. It named the risk that at a 64th the two ends are close and the price’s error might be larger than their difference. At a 64th the rule is right; the difference there is half a move, and the price of the end it rejected is right to the hundredth.

The rule fails at a 256th and at a 1,024th with a short reach, both times by pricing the pieces sent straight in too low, because each piece crosses the pieces before it as well as the head. The question the section asked, whether the rotation a merge will do can be priced without doing it, has a plain answer: yes, from a table measured once per buffer. The measurement the sort could not afford to take was not the rotation. It was how many keys each piece would find in front of it once the pieces before it had joined the head.

What the prices assume

Runs spread at random. Every calibration run’s keys are spread uniformly over their range, and so are the tail’s. A tail whose keys cluster would cross a head differently from the table’s runs, and the prices would have to be measured on clustered runs to match.

One file size. The table is measured for the file’s own length and the tail’s, and read between grid points in the head keys above. A sort of a different size would need the table at its own lengths.

Two ends only. The rule chooses between sending every piece in and merging them all first, because the earlier essay found that stopping partway was never the cheapest. The same two prices could price every number of rounds, and nothing here tests whether a priced rule would find a partway point that the earlier essay’s estimates missed.

Moves only. The prices count moves, as the earlier pages did. The binary searches that read each piece’s position and the calibration’s own merges are not charged to the sort; the calibration is done once per buffer and file size, and the searches are a few comparisons a piece.

Still open: pieces priced against the head they will meet

The pieces’ price was short by the keys of the pieces merged before each one. Those keys are known before anything is merged: the pieces are already sorted, and the number of keys in an earlier piece above a later piece’s smallest is one binary search of that earlier piece. A piece’s true AA is the original head’s count above its smallest plus the sum over earlier pieces, and with that AA the same table should price each crossing as closely as it prices the tail’s.

The measurement that follows prices each piece at that AA, the head’s count plus the earlier pieces’ counts, and repeats every buffer and reach. The prediction is that the pieces sent straight in are then priced within 5% at every buffer, so that the rule chooses the cheaper end at all seven buffers and all ten reach settings. It could fail on the order of the pieces. The searches cost a binary search of every earlier piece for every piece, which at a 4,096th is 410 pieces and eighty thousand searches, and it could fail on the table: a piece that fits the buffer merges by shifting, and if its crossings of earlier pieces scatter it through the head in a way a single run’s crossing does not, the table’s price for one run may not hold for the many short crossings a piece makes. The question is whether the sequence of merges can be priced as a sum of single merges once each is given the head it will really meet, or whether merging pieces one after another is a different process from merging one of them.

What this makes readable

Essays that name this one as a prerequisite.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

Every essay whose body links to this one.

The objects this essay names

Each one links to every other essay that touches it.

Auxiliary spaceBinary searchData movementMerge sortPredictionRotationStable sort