{"about":"Lesson A26, Search and sorting: the cost of looking. Computed by tools/school/fill_search.mjs from system/school/search.mjs and search-steps.mjs. Do not edit by hand. Comparison and move counts are exact and the same on every machine; wall times are this machine only. No GPU.","sourceSha256":{"search.mjs":"d871451d4a77603a07dd2b2e51caa53ab065269c926b878160880c6ea6065795","search-steps.mjs":"5b6fcb97fae306ad6c9627802c9a55c4a49b39b2376b483650cc00308eebd8ca"},"recipe":{"sizes":[8,65536],"seeds":[1,10],"insertionStopsAt":65536,"linearLookupsPerSeed":200},"sources":{"about":"What the sources are cited for and how far each was checked. Every count in the other blocks is this lesson's own measurement on its own code (system/school/search.mjs); the closed forms are checked against those counts exactly, which is what makes them trustworthy here, not the citation. Where a source's text could not be read, the entry says so and gives what was checked instead.","knuthVol3":{"cite":"Donald E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching, second edition, Addison-Wesley, 1998 (ISBN 0-201-89685-0)","read":"Not read: the book is not freely available. Checked: a bookseller's copy of the publisher's table of contents lists 'Sorting by Insertion' (page 80) and 'Sorting by Merging' (page 158) under internal sorting; the 5.2.1 and 5.2.4 numbers are the book's usual numbering and were not seen in that listing. Section 6.2.1 ('Searching an Ordered Table', Algorithm B, binary search) was not seen in any table of contents; the UNIX System III bsearch manual page and the illumos bsearch.c header (seen in web search results, not opened) state they are 'generalized from Knuth (6.2.1) Algorithm B', which confirms the section and algorithm name, not the bound.","citedFor":["5.2.1: straight insertion; the link between the moves it makes and the inversions of the input (unverified wording)","5.2.4: merge sorting (unverified wording; the worst-case count n log2 n - n + 1 for n a power of two is derived and measured here, not quoted)","6.2.1: Algorithm B, binary search with three-way comparisons; at most floor(log2 n) + 1 comparisons for a successful search (unverified wording; measured here exactly for every key)"]},"clrs":{"cite":"Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, MIT Press, 2022 (ISBN 978-0-262-04630-5), chapter 2 Getting Started","read":"Table of contents checked from a copy of the front matter: 2.1 Insertion sort (page 17), 2.2 Analyzing algorithms (page 25), 2.3 Designing algorithms (page 34, merge sort). The text of the sections was not read for this lesson.","citedFor":["2.1 and 2.2: insertion sort, its best case on sorted input and its worst case on reversed input; the book counts running time in its own cost model, so the exact comparison counts n - 1 and n(n-1)/2 are derived here and checked by the counters, not quoted","2.3: merge sort by divide and conquer"]},"derived":{"about":"Closed forms derived for this lesson, each checked exactly against the counters in search.test.mjs and in the closedForms block.","forms":["binary search average over all n successful searches = 1 + ((n + 1) q - 2^(q+1) + 2) / n, q = floor(log2 n): the tree of probes has its leaves on two adjacent levels, so its internal path length is the minimum, sum over k = 1..n of floor(log2 k)","insertion sort comparisons = inversions + (n - 1) - (keys that travel all the way to the front); on a random permutation of distinct keys the expectation is n(n-1)/4 + n - H_n, H_n the n-th harmonic number","merge sort with n = 2^k: merging two runs of m takes between m and 2m - 1 comparisons, so the total lies between (n/2) log2 n and n log2 n - n + 1, and every level writes n elements, n log2 n moves"]}},"inputs":{"about":"Seeded inputs (makeInput in search.mjs, the shared mulberry32 generator rng from tinyformer-ops.mjs): random is a Fisher-Yates permutation of 0..n-1; sorted and reversed are 0..n-1 forwards and backwards; few draws each value from 0 to 3. Sizes are the powers of two from 8 to 65,536.","sizes":[8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536],"seeds":[1,2,3,4,5,6,7,8,9,10],"kinds":["random","sorted","reversed","few"]},"search":{"about":"Searching n keys. binary: a sorted array of the even numbers 0, 2, .., 2(n - 1); successful searches for every key (n of them), unsuccessful for every odd gap from -1 to 2n - 1 (n + 1 of them); mean, min and max comparisons (one three-way comparison per probe), against the bound floor(log2 n) + 1 and the exact average 1 + ((n + 1) q - 2^(q+1) + 2) / n. linear: a random order of 0..n-1 for each of seeds 1 to 10, 200 keys drawn at random per seed; mean, min and max are over the per-seed means, against (n + 1) / 2; a missing key always costs n.","binary":[{"n":8,"successful":{"mean":2.625,"min":1,"max":4,"searches":8},"unsuccessful":{"mean":3.2222,"min":3,"max":4,"searches":9},"bound":4,"exactAverage":2.625},{"n":16,"successful":{"mean":3.375,"min":1,"max":5,"searches":16},"unsuccessful":{"mean":4.1176,"min":4,"max":5,"searches":17},"bound":5,"exactAverage":3.375},{"n":32,"successful":{"mean":4.2188,"min":1,"max":6,"searches":32},"unsuccessful":{"mean":5.0606,"min":5,"max":6,"searches":33},"bound":6,"exactAverage":4.2188},{"n":64,"successful":{"mean":5.125,"min":1,"max":7,"searches":64},"unsuccessful":{"mean":6.0308,"min":6,"max":7,"searches":65},"bound":7,"exactAverage":5.125},{"n":128,"successful":{"mean":6.0703,"min":1,"max":8,"searches":128},"unsuccessful":{"mean":7.0155,"min":7,"max":8,"searches":129},"bound":8,"exactAverage":6.0703},{"n":256,"successful":{"mean":7.0391,"min":1,"max":9,"searches":256},"unsuccessful":{"mean":8.0078,"min":8,"max":9,"searches":257},"bound":9,"exactAverage":7.0391},{"n":512,"successful":{"mean":8.0215,"min":1,"max":10,"searches":512},"unsuccessful":{"mean":9.0039,"min":9,"max":10,"searches":513},"bound":10,"exactAverage":8.0215},{"n":1024,"successful":{"mean":9.0117,"min":1,"max":11,"searches":1024},"unsuccessful":{"mean":10.002,"min":10,"max":11,"searches":1025},"bound":11,"exactAverage":9.0117},{"n":2048,"successful":{"mean":10.0063,"min":1,"max":12,"searches":2048},"unsuccessful":{"mean":11.001,"min":11,"max":12,"searches":2049},"bound":12,"exactAverage":10.0063},{"n":4096,"successful":{"mean":11.0034,"min":1,"max":13,"searches":4096},"unsuccessful":{"mean":12.0005,"min":12,"max":13,"searches":4097},"bound":13,"exactAverage":11.0034},{"n":8192,"successful":{"mean":12.0018,"min":1,"max":14,"searches":8192},"unsuccessful":{"mean":13.0002,"min":13,"max":14,"searches":8193},"bound":14,"exactAverage":12.0018},{"n":16384,"successful":{"mean":13.001,"min":1,"max":15,"searches":16384},"unsuccessful":{"mean":14.0001,"min":14,"max":15,"searches":16385},"bound":15,"exactAverage":13.001},{"n":32768,"successful":{"mean":14.0005,"min":1,"max":16,"searches":32768},"unsuccessful":{"mean":15.0001,"min":15,"max":16,"searches":32769},"bound":16,"exactAverage":14.0005},{"n":65536,"successful":{"mean":15.0003,"min":1,"max":17,"searches":65536},"unsuccessful":{"mean":16,"min":16,"max":17,"searches":65537},"bound":17,"exactAverage":15.0003}],"linear":[{"n":8,"successful":{"mean":4.4285,"min":4.145,"max":4.665},"expected":4.5,"missing":8,"best":1,"worst":8},{"n":16,"successful":{"mean":8.474,"min":7.99,"max":9.085},"expected":8.5,"missing":16,"best":1,"worst":16},{"n":32,"successful":{"mean":16.423,"min":14.74,"max":17.555},"expected":16.5,"missing":32,"best":1,"worst":32},{"n":64,"successful":{"mean":32.2515,"min":30.415,"max":34.47},"expected":32.5,"missing":64,"best":1,"worst":64},{"n":128,"successful":{"mean":65.08,"min":61.32,"max":71.975},"expected":64.5,"missing":128,"best":1,"worst":128},{"n":256,"successful":{"mean":127.5835,"min":115.815,"max":135.81},"expected":128.5,"missing":256,"best":1,"worst":256},{"n":512,"successful":{"mean":253.1615,"min":239.965,"max":281.12},"expected":256.5,"missing":512,"best":1,"worst":512},{"n":1024,"successful":{"mean":505.512,"min":474.19,"max":527.335},"expected":512.5,"missing":1024,"best":1,"worst":1024},{"n":2048,"successful":{"mean":1046.1365,"min":942.44,"max":1117.48},"expected":1024.5,"missing":2048,"best":1,"worst":2048},{"n":4096,"successful":{"mean":2057.384,"min":1927.22,"max":2150.725},"expected":2048.5,"missing":4096,"best":1,"worst":4096},{"n":8192,"successful":{"mean":4121.1295,"min":3929.175,"max":4381.82},"expected":4096.5,"missing":8192,"best":1,"worst":8192},{"n":16384,"successful":{"mean":8088.731,"min":7887.395,"max":8253.925},"expected":8192.5,"missing":16384,"best":1,"worst":16384},{"n":32768,"successful":{"mean":16436.862,"min":16018.17,"max":17299.03},"expected":16384.5,"missing":32768,"best":1,"worst":32768},{"n":65536,"successful":{"mean":32921.391,"min":31187.53,"max":35347.155},"expected":32768.5,"missing":65536,"best":1,"worst":65536}],"verdict":"At n = 65536: binary search takes at most 17 comparisons (bound 17), 15 on average; linear search takes 32921.4 on average (seed means 31187.5 to 35347.2, expected 32768.5) and 65536 for a missing key."},"sorting":{"about":"Sorting n keys of each kind (random: a permutation of 0..n-1; sorted; reversed; few: values 0 to 3), comparisons and moves as defined in search.mjs. Random and few use seeds 1 to 10 (mean, min, max over seeds); sorted and reversed are the same for every seed, so one run. Insertion sort runs to n = 65536 (no stop was needed: one random run there took 1261 ms on this machine; its work grows as n^2, so each doubling of n costs about 4 times as much). insertionRandom compares the measured mean on random input with the derived expectation n(n-1)/4 + n - H_n. merge sort runs to n = 65536.","insertionStopsAt":65536,"msOneRandomRunAtStop":1260.7706,"rows":[{"kind":"random","n":8,"seeds":10,"insertion":{"comparisons":{"mean":20,"min":13,"max":24},"moves":{"mean":20.8,"min":11,"max":28}},"merge":{"comparisons":{"mean":15.7,"min":14,"max":17},"moves":{"mean":24,"min":24,"max":24}}},{"kind":"random","n":16,"seeds":10,"insertion":{"comparisons":{"mean":74.8,"min":67,"max":91},"moves":{"mean":75.4,"min":66,"max":91}},"merge":{"comparisons":{"mean":45,"min":43,"max":47},"moves":{"mean":64,"min":64,"max":64}}},{"kind":"random","n":32,"seeds":10,"insertion":{"comparisons":{"mean":274.8,"min":227,"max":338},"moves":{"mean":276.3,"min":228,"max":341}},"merge":{"comparisons":{"mean":122.8,"min":117,"max":125},"moves":{"mean":160,"min":160,"max":160}}},{"kind":"random","n":64,"seeds":10,"insertion":{"comparisons":{"mean":1105.3,"min":1043,"max":1214},"moves":{"mean":1105.8,"min":1042,"max":1219}},"merge":{"comparisons":{"mean":304.6,"min":301,"max":308},"moves":{"mean":384,"min":384,"max":384}}},{"kind":"random","n":128,"seeds":10,"insertion":{"comparisons":{"mean":4160.6,"min":3822,"max":4628},"moves":{"mean":4160.1,"min":3822,"max":4631}},"merge":{"comparisons":{"mean":735.2,"min":728,"max":743},"moves":{"mean":896,"min":896,"max":896}}},{"kind":"random","n":256,"seeds":10,"insertion":{"comparisons":{"mean":16666.6,"min":15162,"max":17253},"moves":{"mean":16665.1,"min":15159,"max":17251}},"merge":{"comparisons":{"mean":1724.8,"min":1700,"max":1740},"moves":{"mean":2048,"min":2048,"max":2048}}},{"kind":"random","n":512,"seeds":10,"insertion":{"comparisons":{"mean":66152.5,"min":61158,"max":69085},"moves":{"mean":66152.7,"min":61158,"max":69086}},"merge":{"comparisons":{"mean":3959.8,"min":3938,"max":3979},"moves":{"mean":4608,"min":4608,"max":4608}}},{"kind":"random","n":1024,"seeds":10,"insertion":{"comparisons":{"mean":260739.6,"min":248047,"max":269935},"moves":{"mean":260740.1,"min":248044,"max":269939}},"merge":{"comparisons":{"mean":8951.5,"min":8924,"max":9006},"moves":{"mean":10240,"min":10240,"max":10240}}},{"kind":"random","n":2048,"seeds":10,"insertion":{"comparisons":{"mean":1042321.4,"min":1005107,"max":1069170},"moves":{"mean":1042322,"min":1005109,"max":1069167}},"merge":{"comparisons":{"mean":19942.9,"min":19906,"max":19990},"moves":{"mean":22528,"min":22528,"max":22528}}},{"kind":"random","n":4096,"seeds":10,"insertion":{"comparisons":{"mean":4192377.8,"min":4129542,"max":4279739},"moves":{"mean":4192377,"min":4129539,"max":4279739}},"merge":{"comparisons":{"mean":43967.3,"min":43894,"max":44007},"moves":{"mean":49152,"min":49152,"max":49152}}},{"kind":"random","n":8192,"seeds":10,"insertion":{"comparisons":{"mean":16794252.3,"min":16550066,"max":16991275},"moves":{"mean":16794250.3,"min":16550057,"max":16991272}},"merge":{"comparisons":{"mean":96156,"min":96085,"max":96248},"moves":{"mean":106496,"min":106496,"max":106496}}},{"kind":"random","n":16384,"seeds":10,"insertion":{"comparisons":{"mean":67240109.7,"min":66273529,"max":67619108},"moves":{"mean":67240108.3,"min":66273526,"max":67619110}},"merge":{"comparisons":{"mean":208730.9,"min":208660,"max":208767},"moves":{"mean":229376,"min":229376,"max":229376}}},{"kind":"random","n":32768,"seeds":10,"insertion":{"comparisons":{"mean":268687884,"min":267627812,"max":270016295},"moves":{"mean":268687884.5,"min":267627811,"max":270016293}},"merge":{"comparisons":{"mean":450136,"min":450039,"max":450181},"moves":{"mean":491520,"min":491520,"max":491520}}},{"kind":"random","n":65536,"seeds":10,"insertion":{"comparisons":{"mean":1074190182.4,"min":1068914877,"max":1077523739},"moves":{"mean":1074190183,"min":1068914874,"max":1077523744}},"merge":{"comparisons":{"mean":965754.8,"min":965329,"max":966067},"moves":{"mean":1048576,"min":1048576,"max":1048576}}},{"kind":"sorted","n":8,"seeds":1,"insertion":{"comparisons":{"mean":7,"min":7,"max":7},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":12,"min":12,"max":12},"moves":{"mean":24,"min":24,"max":24}}},{"kind":"sorted","n":16,"seeds":1,"insertion":{"comparisons":{"mean":15,"min":15,"max":15},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":32,"min":32,"max":32},"moves":{"mean":64,"min":64,"max":64}}},{"kind":"sorted","n":32,"seeds":1,"insertion":{"comparisons":{"mean":31,"min":31,"max":31},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":80,"min":80,"max":80},"moves":{"mean":160,"min":160,"max":160}}},{"kind":"sorted","n":64,"seeds":1,"insertion":{"comparisons":{"mean":63,"min":63,"max":63},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":192,"min":192,"max":192},"moves":{"mean":384,"min":384,"max":384}}},{"kind":"sorted","n":128,"seeds":1,"insertion":{"comparisons":{"mean":127,"min":127,"max":127},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":448,"min":448,"max":448},"moves":{"mean":896,"min":896,"max":896}}},{"kind":"sorted","n":256,"seeds":1,"insertion":{"comparisons":{"mean":255,"min":255,"max":255},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":1024,"min":1024,"max":1024},"moves":{"mean":2048,"min":2048,"max":2048}}},{"kind":"sorted","n":512,"seeds":1,"insertion":{"comparisons":{"mean":511,"min":511,"max":511},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":2304,"min":2304,"max":2304},"moves":{"mean":4608,"min":4608,"max":4608}}},{"kind":"sorted","n":1024,"seeds":1,"insertion":{"comparisons":{"mean":1023,"min":1023,"max":1023},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":5120,"min":5120,"max":5120},"moves":{"mean":10240,"min":10240,"max":10240}}},{"kind":"sorted","n":2048,"seeds":1,"insertion":{"comparisons":{"mean":2047,"min":2047,"max":2047},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":11264,"min":11264,"max":11264},"moves":{"mean":22528,"min":22528,"max":22528}}},{"kind":"sorted","n":4096,"seeds":1,"insertion":{"comparisons":{"mean":4095,"min":4095,"max":4095},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":24576,"min":24576,"max":24576},"moves":{"mean":49152,"min":49152,"max":49152}}},{"kind":"sorted","n":8192,"seeds":1,"insertion":{"comparisons":{"mean":8191,"min":8191,"max":8191},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":53248,"min":53248,"max":53248},"moves":{"mean":106496,"min":106496,"max":106496}}},{"kind":"sorted","n":16384,"seeds":1,"insertion":{"comparisons":{"mean":16383,"min":16383,"max":16383},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":114688,"min":114688,"max":114688},"moves":{"mean":229376,"min":229376,"max":229376}}},{"kind":"sorted","n":32768,"seeds":1,"insertion":{"comparisons":{"mean":32767,"min":32767,"max":32767},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":245760,"min":245760,"max":245760},"moves":{"mean":491520,"min":491520,"max":491520}}},{"kind":"sorted","n":65536,"seeds":1,"insertion":{"comparisons":{"mean":65535,"min":65535,"max":65535},"moves":{"mean":0,"min":0,"max":0}},"merge":{"comparisons":{"mean":524288,"min":524288,"max":524288},"moves":{"mean":1048576,"min":1048576,"max":1048576}}},{"kind":"reversed","n":8,"seeds":1,"insertion":{"comparisons":{"mean":28,"min":28,"max":28},"moves":{"mean":35,"min":35,"max":35}},"merge":{"comparisons":{"mean":12,"min":12,"max":12},"moves":{"mean":24,"min":24,"max":24}}},{"kind":"reversed","n":16,"seeds":1,"insertion":{"comparisons":{"mean":120,"min":120,"max":120},"moves":{"mean":135,"min":135,"max":135}},"merge":{"comparisons":{"mean":32,"min":32,"max":32},"moves":{"mean":64,"min":64,"max":64}}},{"kind":"reversed","n":32,"seeds":1,"insertion":{"comparisons":{"mean":496,"min":496,"max":496},"moves":{"mean":527,"min":527,"max":527}},"merge":{"comparisons":{"mean":80,"min":80,"max":80},"moves":{"mean":160,"min":160,"max":160}}},{"kind":"reversed","n":64,"seeds":1,"insertion":{"comparisons":{"mean":2016,"min":2016,"max":2016},"moves":{"mean":2079,"min":2079,"max":2079}},"merge":{"comparisons":{"mean":192,"min":192,"max":192},"moves":{"mean":384,"min":384,"max":384}}},{"kind":"reversed","n":128,"seeds":1,"insertion":{"comparisons":{"mean":8128,"min":8128,"max":8128},"moves":{"mean":8255,"min":8255,"max":8255}},"merge":{"comparisons":{"mean":448,"min":448,"max":448},"moves":{"mean":896,"min":896,"max":896}}},{"kind":"reversed","n":256,"seeds":1,"insertion":{"comparisons":{"mean":32640,"min":32640,"max":32640},"moves":{"mean":32895,"min":32895,"max":32895}},"merge":{"comparisons":{"mean":1024,"min":1024,"max":1024},"moves":{"mean":2048,"min":2048,"max":2048}}},{"kind":"reversed","n":512,"seeds":1,"insertion":{"comparisons":{"mean":130816,"min":130816,"max":130816},"moves":{"mean":131327,"min":131327,"max":131327}},"merge":{"comparisons":{"mean":2304,"min":2304,"max":2304},"moves":{"mean":4608,"min":4608,"max":4608}}},{"kind":"reversed","n":1024,"seeds":1,"insertion":{"comparisons":{"mean":523776,"min":523776,"max":523776},"moves":{"mean":524799,"min":524799,"max":524799}},"merge":{"comparisons":{"mean":5120,"min":5120,"max":5120},"moves":{"mean":10240,"min":10240,"max":10240}}},{"kind":"reversed","n":2048,"seeds":1,"insertion":{"comparisons":{"mean":2096128,"min":2096128,"max":2096128},"moves":{"mean":2098175,"min":2098175,"max":2098175}},"merge":{"comparisons":{"mean":11264,"min":11264,"max":11264},"moves":{"mean":22528,"min":22528,"max":22528}}},{"kind":"reversed","n":4096,"seeds":1,"insertion":{"comparisons":{"mean":8386560,"min":8386560,"max":8386560},"moves":{"mean":8390655,"min":8390655,"max":8390655}},"merge":{"comparisons":{"mean":24576,"min":24576,"max":24576},"moves":{"mean":49152,"min":49152,"max":49152}}},{"kind":"reversed","n":8192,"seeds":1,"insertion":{"comparisons":{"mean":33550336,"min":33550336,"max":33550336},"moves":{"mean":33558527,"min":33558527,"max":33558527}},"merge":{"comparisons":{"mean":53248,"min":53248,"max":53248},"moves":{"mean":106496,"min":106496,"max":106496}}},{"kind":"reversed","n":16384,"seeds":1,"insertion":{"comparisons":{"mean":134209536,"min":134209536,"max":134209536},"moves":{"mean":134225919,"min":134225919,"max":134225919}},"merge":{"comparisons":{"mean":114688,"min":114688,"max":114688},"moves":{"mean":229376,"min":229376,"max":229376}}},{"kind":"reversed","n":32768,"seeds":1,"insertion":{"comparisons":{"mean":536854528,"min":536854528,"max":536854528},"moves":{"mean":536887295,"min":536887295,"max":536887295}},"merge":{"comparisons":{"mean":245760,"min":245760,"max":245760},"moves":{"mean":491520,"min":491520,"max":491520}}},{"kind":"reversed","n":65536,"seeds":1,"insertion":{"comparisons":{"mean":2147450880,"min":2147450880,"max":2147450880},"moves":{"mean":2147516415,"min":2147516415,"max":2147516415}},"merge":{"comparisons":{"mean":524288,"min":524288,"max":524288},"moves":{"mean":1048576,"min":1048576,"max":1048576}}},{"kind":"few","n":8,"seeds":10,"insertion":{"comparisons":{"mean":18,"min":15,"max":26},"moves":{"mean":16.7,"min":13,"max":26}},"merge":{"comparisons":{"mean":15.9,"min":13,"max":17},"moves":{"mean":24,"min":24,"max":24}}},{"kind":"few","n":16,"seeds":10,"insertion":{"comparisons":{"mean":61.6,"min":45,"max":85},"moves":{"mean":58.5,"min":44,"max":82}},"merge":{"comparisons":{"mean":45.7,"min":42,"max":48},"moves":{"mean":64,"min":64,"max":64}}},{"kind":"few","n":32,"seeds":10,"insertion":{"comparisons":{"mean":219.9,"min":155,"max":256},"moves":{"mean":212.7,"min":142,"max":251}},"merge":{"comparisons":{"mean":118.9,"min":110,"max":123},"moves":{"mean":160,"min":160,"max":160}}},{"kind":"few","n":64,"seeds":10,"insertion":{"comparisons":{"mean":780.6,"min":643,"max":875},"moves":{"mean":764.5,"min":628,"max":856}},"merge":{"comparisons":{"mean":290.6,"min":280,"max":298},"moves":{"mean":384,"min":384,"max":384}}},{"kind":"few","n":128,"seeds":10,"insertion":{"comparisons":{"mean":3108.3,"min":2569,"max":3413},"moves":{"mean":3075.1,"min":2531,"max":3383}},"merge":{"comparisons":{"mean":691.5,"min":673,"max":707},"moves":{"mean":896,"min":896,"max":896}}},{"kind":"few","n":256,"seeds":10,"insertion":{"comparisons":{"mean":12378.7,"min":11574,"max":13273},"moves":{"mean":12311.8,"min":11499,"max":13209}},"merge":{"comparisons":{"mean":1603.8,"min":1573,"max":1629},"moves":{"mean":2048,"min":2048,"max":2048}}},{"kind":"few","n":512,"seeds":10,"insertion":{"comparisons":{"mean":49990.8,"min":47890,"max":52760},"moves":{"mean":49859.6,"min":47766,"max":52633}},"merge":{"comparisons":{"mean":3660.1,"min":3611,"max":3691},"moves":{"mean":4608,"min":4608,"max":4608}}},{"kind":"few","n":1024,"seeds":10,"insertion":{"comparisons":{"mean":200000.1,"min":193784,"max":208581},"moves":{"mean":199739,"min":193542,"max":208315}},"merge":{"comparisons":{"mean":8214.2,"min":8175,"max":8284},"moves":{"mean":10240,"min":10240,"max":10240}}},{"kind":"few","n":2048,"seeds":10,"insertion":{"comparisons":{"mean":797772.3,"min":773882,"max":826312},"moves":{"mean":797264.7,"min":773350,"max":825821}},"merge":{"comparisons":{"mean":18273.8,"min":18181,"max":18399},"moves":{"mean":22528,"min":22528,"max":22528}}},{"kind":"few","n":4096,"seeds":10,"insertion":{"comparisons":{"mean":3175253.8,"min":3118175,"max":3229454},"moves":{"mean":3174251.1,"min":3117181,"max":3228494}},"merge":{"comparisons":{"mean":40185.7,"min":40063,"max":40349},"moves":{"mean":49152,"min":49152,"max":49152}}},{"kind":"few","n":8192,"seeds":10,"insertion":{"comparisons":{"mean":12571340,"min":12425495,"max":12799590},"moves":{"mean":12569304.7,"min":12423450,"max":12797565}},"merge":{"comparisons":{"mean":87395.5,"min":87180,"max":87729},"moves":{"mean":106496,"min":106496,"max":106496}}},{"kind":"few","n":16384,"seeds":10,"insertion":{"comparisons":{"mean":50389474.8,"min":49872906,"max":50805559},"moves":{"mean":50385407,"min":49868772,"max":50801539}},"merge":{"comparisons":{"mean":189163.8,"min":188759,"max":189848},"moves":{"mean":229376,"min":229376,"max":229376}}},{"kind":"few","n":32768,"seeds":10,"insertion":{"comparisons":{"mean":201304152,"min":200598163,"max":203033311},"moves":{"mean":201295984.5,"min":200590160,"max":203025095}},"merge":{"comparisons":{"mean":406737.6,"min":406277,"max":407651},"moves":{"mean":491520,"min":491520,"max":491520}}},{"kind":"few","n":65536,"seeds":10,"insertion":{"comparisons":{"mean":804255410.1,"min":798807634,"max":810669028},"moves":{"mean":804239016.1,"min":798791224,"max":810652662}},"merge":{"comparisons":{"mean":870384.4,"min":869444,"max":871674},"moves":{"mean":1048576,"min":1048576,"max":1048576}}}],"insertionRandom":[{"n":8,"measured":20,"expected":19.2821},{"n":16,"measured":74.8,"expected":72.6193},{"n":32,"measured":274.8,"expected":275.9415},{"n":64,"measured":1105.3,"expected":1067.2561},{"n":128,"measured":4160.6,"expected":4186.5669},{"n":256,"measured":16666.6,"expected":16569.8757},{"n":512,"measured":66152.5,"expected":65913.1835},{"n":1024,"measured":260739.6,"expected":262904.4908},{"n":2048,"measured":1042321.4,"expected":1050103.7979},{"n":4096,"measured":4192377.8,"expected":4197367.1049},{"n":8192,"measured":16794252.3,"expected":16783350.4118},{"n":16384,"measured":67240109.7,"expected":67121141.7187},{"n":32768,"measured":268687884,"expected":268460021.0256},{"n":65536,"measured":1074190182.4,"expected":1073790964.3324}],"verdict":"At n = 65536, random input: insertion sort 1074190182 comparisons (expected 1073790964), merge sort 965755. Reversed: insertion 2147450880 = n(n-1)/2; sorted: 65535 = n - 1. Merge sort at n = 65536: 965755 comparisons on random input (bounds 524288 and 983041)."},"closedForms":{"about":"Every closed form against the counters, exactly (holds is strict equality, or for the binary average agreement to 1e-9, or for ranges containment). The binary-search rows use every successful key, so max is the true maximum, not a sample. Unsuccessful searches are only bounded above here.","rows":[{"form":"binary search max = floor(log2 n) + 1","n":8,"expected":4,"measured":4,"holds":true},{"form":"binary search average (exact)","n":8,"expected":2.625,"measured":2.625,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":8,"expected":4,"measured":4,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":16,"expected":5,"measured":5,"holds":true},{"form":"binary search average (exact)","n":16,"expected":3.375,"measured":3.375,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":16,"expected":5,"measured":5,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":32,"expected":6,"measured":6,"holds":true},{"form":"binary search average (exact)","n":32,"expected":4.2188,"measured":4.2188,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":32,"expected":6,"measured":6,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":64,"expected":7,"measured":7,"holds":true},{"form":"binary search average (exact)","n":64,"expected":5.125,"measured":5.125,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":64,"expected":7,"measured":7,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":128,"expected":8,"measured":8,"holds":true},{"form":"binary search average (exact)","n":128,"expected":6.0703,"measured":6.0703,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":128,"expected":8,"measured":8,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":256,"expected":9,"measured":9,"holds":true},{"form":"binary search average (exact)","n":256,"expected":7.0391,"measured":7.0391,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":256,"expected":9,"measured":9,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":512,"expected":10,"measured":10,"holds":true},{"form":"binary search average (exact)","n":512,"expected":8.0215,"measured":8.0215,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":512,"expected":10,"measured":10,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":1024,"expected":11,"measured":11,"holds":true},{"form":"binary search average (exact)","n":1024,"expected":9.0117,"measured":9.0117,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":1024,"expected":11,"measured":11,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":2048,"expected":12,"measured":12,"holds":true},{"form":"binary search average (exact)","n":2048,"expected":10.0063,"measured":10.0063,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":2048,"expected":12,"measured":12,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":4096,"expected":13,"measured":13,"holds":true},{"form":"binary search average (exact)","n":4096,"expected":11.0034,"measured":11.0034,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":4096,"expected":13,"measured":13,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":8192,"expected":14,"measured":14,"holds":true},{"form":"binary search average (exact)","n":8192,"expected":12.0018,"measured":12.0018,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":8192,"expected":14,"measured":14,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":16384,"expected":15,"measured":15,"holds":true},{"form":"binary search average (exact)","n":16384,"expected":13.001,"measured":13.001,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":16384,"expected":15,"measured":15,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":32768,"expected":16,"measured":16,"holds":true},{"form":"binary search average (exact)","n":32768,"expected":14.0005,"measured":14.0005,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":32768,"expected":16,"measured":16,"holds":true},{"form":"binary search max = floor(log2 n) + 1","n":65536,"expected":17,"measured":17,"holds":true},{"form":"binary search average (exact)","n":65536,"expected":15.0003,"measured":15.0003,"holds":true},{"form":"binary search, unsuccessful, at most floor(log2 n) + 1","n":65536,"expected":17,"measured":17,"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":8,"expected":28,"measured":28,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":8,"expected":7,"measured":7,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":8,"expected":12,"measured":12,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":8,"expected":17,"measured":17,"holds":true},{"form":"merge moves = n log2 n (random)","n":8,"expected":24,"measured":24,"holds":true},{"form":"merge random and few within [best, worst]","n":8,"expected":[12,17],"measured":[13,17],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":16,"expected":120,"measured":120,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":16,"expected":15,"measured":15,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":16,"expected":32,"measured":32,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":16,"expected":49,"measured":49,"holds":true},{"form":"merge moves = n log2 n (random)","n":16,"expected":64,"measured":64,"holds":true},{"form":"merge random and few within [best, worst]","n":16,"expected":[32,49],"measured":[42,48],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":32,"expected":496,"measured":496,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":32,"expected":31,"measured":31,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":32,"expected":80,"measured":80,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":32,"expected":129,"measured":129,"holds":true},{"form":"merge moves = n log2 n (random)","n":32,"expected":160,"measured":160,"holds":true},{"form":"merge random and few within [best, worst]","n":32,"expected":[80,129],"measured":[110,125],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":64,"expected":2016,"measured":2016,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":64,"expected":63,"measured":63,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":64,"expected":192,"measured":192,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":64,"expected":321,"measured":321,"holds":true},{"form":"merge moves = n log2 n (random)","n":64,"expected":384,"measured":384,"holds":true},{"form":"merge random and few within [best, worst]","n":64,"expected":[192,321],"measured":[280,308],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":128,"expected":8128,"measured":8128,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":128,"expected":127,"measured":127,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":128,"expected":448,"measured":448,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":128,"expected":769,"measured":769,"holds":true},{"form":"merge moves = n log2 n (random)","n":128,"expected":896,"measured":896,"holds":true},{"form":"merge random and few within [best, worst]","n":128,"expected":[448,769],"measured":[673,743],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":256,"expected":32640,"measured":32640,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":256,"expected":255,"measured":255,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":256,"expected":1024,"measured":1024,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":256,"expected":1793,"measured":1793,"holds":true},{"form":"merge moves = n log2 n (random)","n":256,"expected":2048,"measured":2048,"holds":true},{"form":"merge random and few within [best, worst]","n":256,"expected":[1024,1793],"measured":[1573,1740],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":512,"expected":130816,"measured":130816,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":512,"expected":511,"measured":511,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":512,"expected":2304,"measured":2304,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":512,"expected":4097,"measured":4097,"holds":true},{"form":"merge moves = n log2 n (random)","n":512,"expected":4608,"measured":4608,"holds":true},{"form":"merge random and few within [best, worst]","n":512,"expected":[2304,4097],"measured":[3611,3979],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":1024,"expected":523776,"measured":523776,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":1024,"expected":1023,"measured":1023,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":1024,"expected":5120,"measured":5120,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":1024,"expected":9217,"measured":9217,"holds":true},{"form":"merge moves = n log2 n (random)","n":1024,"expected":10240,"measured":10240,"holds":true},{"form":"merge random and few within [best, worst]","n":1024,"expected":[5120,9217],"measured":[8175,9006],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":2048,"expected":2096128,"measured":2096128,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":2048,"expected":2047,"measured":2047,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":2048,"expected":11264,"measured":11264,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":2048,"expected":20481,"measured":20481,"holds":true},{"form":"merge moves = n log2 n (random)","n":2048,"expected":22528,"measured":22528,"holds":true},{"form":"merge random and few within [best, worst]","n":2048,"expected":[11264,20481],"measured":[18181,19990],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":4096,"expected":8386560,"measured":8386560,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":4096,"expected":4095,"measured":4095,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":4096,"expected":24576,"measured":24576,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":4096,"expected":45057,"measured":45057,"holds":true},{"form":"merge moves = n log2 n (random)","n":4096,"expected":49152,"measured":49152,"holds":true},{"form":"merge random and few within [best, worst]","n":4096,"expected":[24576,45057],"measured":[40063,44007],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":8192,"expected":33550336,"measured":33550336,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":8192,"expected":8191,"measured":8191,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":8192,"expected":53248,"measured":53248,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":8192,"expected":98305,"measured":98305,"holds":true},{"form":"merge moves = n log2 n (random)","n":8192,"expected":106496,"measured":106496,"holds":true},{"form":"merge random and few within [best, worst]","n":8192,"expected":[53248,98305],"measured":[87180,96248],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":16384,"expected":134209536,"measured":134209536,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":16384,"expected":16383,"measured":16383,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":16384,"expected":114688,"measured":114688,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":16384,"expected":212993,"measured":212993,"holds":true},{"form":"merge moves = n log2 n (random)","n":16384,"expected":229376,"measured":229376,"holds":true},{"form":"merge random and few within [best, worst]","n":16384,"expected":[114688,212993],"measured":[188759,208767],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":32768,"expected":536854528,"measured":536854528,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":32768,"expected":32767,"measured":32767,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":32768,"expected":245760,"measured":245760,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":32768,"expected":458753,"measured":458753,"holds":true},{"form":"merge moves = n log2 n (random)","n":32768,"expected":491520,"measured":491520,"holds":true},{"form":"merge random and few within [best, worst]","n":32768,"expected":[245760,458753],"measured":[406277,450181],"holds":true},{"form":"insertion worst = n(n-1)/2 (reversed)","n":65536,"expected":2147450880,"measured":2147450880,"holds":true},{"form":"insertion best = n - 1 (sorted)","n":65536,"expected":65535,"measured":65535,"holds":true},{"form":"merge best = (n/2) log2 n (sorted)","n":65536,"expected":524288,"measured":524288,"holds":true},{"form":"merge worst = n log2 n - n + 1 (worst input)","n":65536,"expected":983041,"measured":983041,"holds":true},{"form":"merge moves = n log2 n (random)","n":65536,"expected":1048576,"measured":1048576,"holds":true},{"form":"merge random and few within [best, worst]","n":65536,"expected":[524288,983041],"measured":[869444,966067],"holds":true},{"form":"insertion shifts = inversions (random)","n":8,"expected":14,"measured":14,"holds":true},{"form":"insertion shifts = inversions (few)","n":8,"expected":9,"measured":9,"holds":true},{"form":"insertion shifts = inversions (random)","n":64,"expected":1063,"measured":1063,"holds":true},{"form":"insertion shifts = inversions (few)","n":64,"expected":701,"measured":701,"holds":true},{"form":"insertion shifts = inversions (random)","n":512,"expected":65184,"measured":65184,"holds":true},{"form":"insertion shifts = inversions (few)","n":512,"expected":48190,"measured":48190,"holds":true},{"form":"insertion shifts = inversions (random)","n":4096,"expected":4194333,"measured":4194333,"holds":true},{"form":"insertion shifts = inversions (few)","n":4096,"expected":3114081,"measured":3114081,"holds":true}],"allHold":true,"count":134},"steps":{"about":"Step forms (system/school/search-steps.mjs): each step() is one comparison and the moves that follow it. Frames record the state before the first step and after each step: binary search for 11 among 0..15 (lo, hi, mid), linear search for 11 in a random order of 0..15 (seed 3), insertion sort and merge sort of a random order of 0..7 (seed 3). agree: the last frame's answer and counts equal the direct function's.","inputs":{"sorted16":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],"random16":[15,13,10,7,12,8,3,2,1,4,5,9,14,6,0,11],"random8":[7,4,1,3,6,2,0,5]},"binary":{"kind":"binary","steps":2,"frames":[{"lo":0,"hi":15,"mid":-1,"comparisons":0,"index":-1},{"lo":8,"hi":15,"mid":7,"comparisons":1,"index":-1},{"lo":8,"hi":15,"mid":11,"comparisons":2,"index":11}]},"linear":{"kind":"linear","steps":16,"frames":[{"i":-1,"comparisons":0,"index":-1},{"i":0,"comparisons":1,"index":-1},{"i":1,"comparisons":2,"index":-1},{"i":2,"comparisons":3,"index":-1},{"i":3,"comparisons":4,"index":-1},{"i":4,"comparisons":5,"index":-1},{"i":5,"comparisons":6,"index":-1},{"i":6,"comparisons":7,"index":-1},{"i":7,"comparisons":8,"index":-1},{"i":8,"comparisons":9,"index":-1},{"i":9,"comparisons":10,"index":-1},{"i":10,"comparisons":11,"index":-1},{"i":11,"comparisons":12,"index":-1},{"i":12,"comparisons":13,"index":-1},{"i":13,"comparisons":14,"index":-1},{"i":14,"comparisons":15,"index":-1},{"i":15,"comparisons":16,"index":15}]},"insertion":{"kind":"insertion","steps":23,"frames":[{"a":[7,4,1,3,6,2,0,5],"i":0,"j":-1,"comparisons":0,"moves":0},{"a":[7,7,1,3,6,2,0,5],"i":1,"j":0,"comparisons":1,"moves":1},{"a":[4,7,7,3,6,2,0,5],"i":2,"j":1,"comparisons":2,"moves":3},{"a":[4,4,7,3,6,2,0,5],"i":2,"j":0,"comparisons":3,"moves":4},{"a":[1,4,7,7,6,2,0,5],"i":3,"j":2,"comparisons":4,"moves":6},{"a":[1,4,4,7,6,2,0,5],"i":3,"j":1,"comparisons":5,"moves":7},{"a":[1,4,4,7,6,2,0,5],"i":3,"j":0,"comparisons":6,"moves":7},{"a":[1,3,4,7,7,2,0,5],"i":4,"j":3,"comparisons":7,"moves":9},{"a":[1,3,4,7,7,2,0,5],"i":4,"j":2,"comparisons":8,"moves":9},{"a":[1,3,4,6,7,7,0,5],"i":5,"j":4,"comparisons":9,"moves":11},{"a":[1,3,4,6,6,7,0,5],"i":5,"j":3,"comparisons":10,"moves":12},{"a":[1,3,4,4,6,7,0,5],"i":5,"j":2,"comparisons":11,"moves":13},{"a":[1,3,3,4,6,7,0,5],"i":5,"j":1,"comparisons":12,"moves":14},{"a":[1,3,3,4,6,7,0,5],"i":5,"j":0,"comparisons":13,"moves":14},{"a":[1,2,3,4,6,7,7,5],"i":6,"j":5,"comparisons":14,"moves":16},{"a":[1,2,3,4,6,6,7,5],"i":6,"j":4,"comparisons":15,"moves":17},{"a":[1,2,3,4,4,6,7,5],"i":6,"j":3,"comparisons":16,"moves":18},{"a":[1,2,3,3,4,6,7,5],"i":6,"j":2,"comparisons":17,"moves":19},{"a":[1,2,2,3,4,6,7,5],"i":6,"j":1,"comparisons":18,"moves":20},{"a":[1,1,2,3,4,6,7,5],"i":6,"j":0,"comparisons":19,"moves":21},{"a":[0,1,2,3,4,6,7,7],"i":7,"j":6,"comparisons":20,"moves":23},{"a":[0,1,2,3,4,6,6,7],"i":7,"j":5,"comparisons":21,"moves":24},{"a":[0,1,2,3,4,6,6,7],"i":7,"j":4,"comparisons":22,"moves":24},{"a":[0,1,2,3,4,5,6,7],"i":7,"j":4,"comparisons":22,"moves":25}]},"merge":{"kind":"merge","steps":17,"frames":[{"a":[7,4,1,3,6,2,0,5],"lo":0,"mid":0,"hi":0,"comparisons":0,"moves":0},{"a":[4,4,1,3,6,2,0,5],"lo":0,"mid":1,"hi":2,"comparisons":1,"moves":1},{"a":[4,7,1,3,6,2,0,5],"lo":2,"mid":3,"hi":4,"comparisons":2,"moves":3},{"a":[1,7,1,3,6,2,0,5],"lo":0,"mid":2,"hi":4,"comparisons":3,"moves":5},{"a":[1,3,1,3,6,2,0,5],"lo":0,"mid":2,"hi":4,"comparisons":4,"moves":6},{"a":[1,3,4,7,2,2,0,5],"lo":4,"mid":5,"hi":6,"comparisons":5,"moves":9},{"a":[1,3,4,7,2,6,0,5],"lo":6,"mid":7,"hi":8,"comparisons":6,"moves":11},{"a":[1,3,4,7,0,6,0,5],"lo":4,"mid":6,"hi":8,"comparisons":7,"moves":13},{"a":[1,3,4,7,0,2,0,5],"lo":4,"mid":6,"hi":8,"comparisons":8,"moves":14},{"a":[1,3,4,7,0,2,5,5],"lo":4,"mid":6,"hi":8,"comparisons":9,"moves":15},{"a":[0,3,4,7,0,2,5,6],"lo":0,"mid":4,"hi":8,"comparisons":10,"moves":17},{"a":[0,1,4,7,0,2,5,6],"lo":0,"mid":4,"hi":8,"comparisons":11,"moves":18},{"a":[0,1,2,7,0,2,5,6],"lo":0,"mid":4,"hi":8,"comparisons":12,"moves":19},{"a":[0,1,2,3,0,2,5,6],"lo":0,"mid":4,"hi":8,"comparisons":13,"moves":20},{"a":[0,1,2,3,4,2,5,6],"lo":0,"mid":4,"hi":8,"comparisons":14,"moves":21},{"a":[0,1,2,3,4,5,5,6],"lo":0,"mid":4,"hi":8,"comparisons":15,"moves":22},{"a":[0,1,2,3,4,5,6,6],"lo":0,"mid":4,"hi":8,"comparisons":16,"moves":23},{"a":[0,1,2,3,4,5,6,7],"lo":0,"mid":4,"hi":8,"comparisons":16,"moves":24}]},"agree":{"binary":true,"linear":true,"insertion":true,"merge":true}},"controls":{"about":"False-success control: binary search run on unsorted input (a random order of 0..n-1), searching for every key, all of which are present. wrongFraction: share of keys reported missing or at the wrong place (mean, min, max over seeds; 3 seeds from n = 16,384, 10 below); wrong: the fewest wrong answers in any seed. maxComparisons stays within floor(log2 n) + 1: the search is still fast, and it is wrong. Fewer comparisons is only a win when the precondition (sorted input) holds. Linear search on the same arrays finds every key (search.test.mjs).","unsortedBinary":{"rows":[{"n":8,"seeds":10,"keys":8,"wrong":4,"wrongFraction":{"mean":0.6125,"min":0.5,"max":0.75},"maxComparisons":4,"bound":4},{"n":16,"seeds":10,"keys":16,"wrong":11,"wrongFraction":{"mean":0.7688,"min":0.6875,"max":0.8125},"maxComparisons":5,"bound":5},{"n":32,"seeds":10,"keys":32,"wrong":26,"wrongFraction":{"mean":0.85,"min":0.8125,"max":0.9063},"maxComparisons":6,"bound":6},{"n":64,"seeds":10,"keys":64,"wrong":57,"wrongFraction":{"mean":0.9109,"min":0.8906,"max":0.9375},"maxComparisons":7,"bound":7},{"n":128,"seeds":10,"keys":128,"wrong":118,"wrongFraction":{"mean":0.9461,"min":0.9219,"max":0.9609},"maxComparisons":8,"bound":8},{"n":256,"seeds":10,"keys":256,"wrong":246,"wrongFraction":{"mean":0.9691,"min":0.9609,"max":0.9805},"maxComparisons":9,"bound":9},{"n":512,"seeds":10,"keys":512,"wrong":501,"wrongFraction":{"mean":0.982,"min":0.9785,"max":0.9863},"maxComparisons":10,"bound":10},{"n":1024,"seeds":10,"keys":1024,"wrong":1011,"wrongFraction":{"mean":0.9902,"min":0.9873,"max":0.9941},"maxComparisons":11,"bound":11},{"n":2048,"seeds":10,"keys":2048,"wrong":2033,"wrongFraction":{"mean":0.9953,"min":0.9927,"max":0.9971},"maxComparisons":12,"bound":12},{"n":4096,"seeds":10,"keys":4096,"wrong":4076,"wrongFraction":{"mean":0.9969,"min":0.9951,"max":0.998},"maxComparisons":13,"bound":13},{"n":8192,"seeds":10,"keys":8192,"wrong":8176,"wrongFraction":{"mean":0.9985,"min":0.998,"max":0.999},"maxComparisons":14,"bound":14},{"n":16384,"seeds":3,"keys":16384,"wrong":16366,"wrongFraction":{"mean":0.9991,"min":0.9989,"max":0.9993},"maxComparisons":15,"bound":15},{"n":32768,"seeds":3,"keys":32768,"wrong":32750,"wrongFraction":{"mean":0.9996,"min":0.9995,"max":0.9997},"maxComparisons":16,"bound":16},{"n":65536,"seeds":3,"keys":65536,"wrong":65515,"wrongFraction":{"mean":0.9998,"min":0.9997,"max":0.9998},"maxComparisons":17,"bound":17}]},"verdict":"Binary search on unsorted input misses 61.3 percent of keys at n = 8 and 99.975 percent at n = 65536, while never using more than floor(log2 n) + 1 comparisons."},"timing":{"about":"Wall time in Node v25.2.1 on this machine only (13th Gen Intel(R) Core(TM) i7-13700KF, win32), one process at below-normal priority, median of 3 or 5 runs, random input seed 1. Searches in microseconds per lookup (1,000 keys; binary repeated 100 times), sorts in milliseconds per sort, including the counting. builtinSortMs is the engine's own typed-array sort, for scale. Another machine, engine or load gives other numbers; the comparison counts do not change.","rows":[{"n":1024,"linearSearchUs":0.3241,"binarySearchUs":0.0437,"insertionSortMs":0.4242,"mergeSortMs":0.0992,"builtinSortMs":0.0282},{"n":4096,"linearSearchUs":2.9458,"binarySearchUs":0.047,"insertionSortMs":6.759,"mergeSortMs":0.4755,"builtinSortMs":0.173},{"n":16384,"linearSearchUs":11.6553,"binarySearchUs":0.067,"insertionSortMs":73.646,"mergeSortMs":2.3695,"builtinSortMs":0.6892},{"n":65536,"linearSearchUs":41.3484,"binarySearchUs":0.0827,"insertionSortMs":1151.1831,"mergeSortMs":7.3449,"builtinSortMs":2.9949}]},"film":{"about":"What the film draws (Rerun knocking on shells one at a time against halving the beach). beach: n shells, the average knocks for a linear walk ((n + 1) / 2) against the most halvings binary search needs. curves: comparisons against n (random input, mean over seeds) for each method, for log-log plots.","beach":{"shells":65536,"linearAverage":32768.5,"linearWorst":65536,"binaryMax":17},"curves":{"linear":[[8,4.4285],[16,8.474],[32,16.423],[64,32.2515],[128,65.08],[256,127.5835],[512,253.1615],[1024,505.512],[2048,1046.1365],[4096,2057.384],[8192,4121.1295],[16384,8088.731],[32768,16436.862],[65536,32921.391]],"binary":[[8,2.625],[16,3.375],[32,4.2188],[64,5.125],[128,6.0703],[256,7.0391],[512,8.0215],[1024,9.0117],[2048,10.0063],[4096,11.0034],[8192,12.0018],[16384,13.001],[32768,14.0005],[65536,15.0003]],"insertion":[[8,20],[16,74.8],[32,274.8],[64,1105.3],[128,4160.6],[256,16666.6],[512,66152.5],[1024,260739.6],[2048,1042321.4],[4096,4192377.8],[8192,16794252.3],[16384,67240109.7],[32768,268687884],[65536,1074190182.4]],"merge":[[8,15.7],[16,45],[32,122.8],[64,304.6],[128,735.2],[256,1724.8],[512,3959.8],[1024,8951.5],[2048,19942.9],[4096,43967.3],[8192,96156],[16384,208730.9],[32768,450136],[65536,965754.8]]}},"cost":{"about":"Milliseconds this fill spent in each part (this machine only), and the fill's wall time in seconds.","totals":{"search":212.4716,"insertion":28660.8032,"merge":393.7371},"fillSeconds":34.771},"seconds":34.8}
