WorksComputer Science
Selecting the 7th Smallest of 16 Elements Takes Exactly 28 Comparisons
Abstract
Let denote the minimum worst-case number of comparisons needed to find the -th smallest of distinct elements. We construct algorithms showing and , improving the previously reported upper bounds of 30 and 31. Together with known lower bounds, these results establish and leave . For 17 elements, we establish and improve the upper bounds for ranks 6 through 9 to 29, 30, 31, and 31, respectively. Every new upper bound has an explicit decision-graph certificate verified by a static checker developed independently of the search. We also derive a lower bound for selection from a partially ordered state by combining the Fussenegger-Gabow weight with its order-reversed counterpart. All new upper bounds and this lower bound are formally verified in Lean 4. For , we show that a lower bound based solely on the total number of decision-tree leaves cannot establish . The algorithms were found using fixed-budget search, information-guided comparison ordering, and proof-number search. Proof-number search was decisive in finding the 28-comparison algorithm; in our experiments, refuting a comparison budget remained substantially more expensive than finding an algorithm within it.
Introduction
Selection asks for the element of a given rank using as few pairwise comparisons as possible. Write for the smallest worst-case number of comparisons needed to find the -th smallest of distinct elements. Reversing the order shows . Classical results include and (Kislitsyn; see [12]), and the general upper bound of Hadian and Sobel [10], .
Asymptotically, selection takes linear time [2], but even the leading constant for the median is open [3, 7, 6].
Small exact values have been determined by computer search, starting with Gasarch, Kelly and Pugh [9] and Oksanen [14, 15]. Dörrer et al. [5] settled all entries for and the first six ranks for . The two remaining 16-element entries had lower bound 28; with the upper bounds reported in [9 Table 3], the ranges were and .
Theorem 1. .
Theorem 2. . Hence .
Theorem 3. . Moreover , , , and .
Our contribution to all three theorems is the upper bounds. The lower bound of Theorem 1 is the entry 28–33 of [5 Table 3], obtained there by computer search; we use it without rederiving it. The lower bounds and follow from the published values and [5] and the transfer lemma of § 2. Table 1 and Table 2 show the previous and new ranges. The table in [5] lists the Hadian–Sobel values 33 and 36 for the two open 16-element entries; the upper bounds 30 and 31 of [9] are smaller and are the baseline we improve.
Table 1. The row of the selection table.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
| before | 16 | 20 | 22 | 25 | 27–28 | 28–31 | 29–33 | 29–34 | 29–35 |
| this paper | 16 | 20 | 22 | 25 | 27 | 28–29 | 29–30 | 29–31 | 29–31 |
Table 2. The row . Lower bounds: classical formulas for , otherwise the row of [5] with the transfer lemma. Previous upper bounds: Hadian–Sobel for and [9 Table 3] for $i \ge 5.
Contributions and evidence
Seven new upper bounds, each witnessed by an explicit certificate (§ 3). A static checker that shares no code with the search accepts every certificate, and the Lean 4 kernel checks a formal proof of every bound.
A description of the 15-comparison opening shared by several of the new algorithms (§ 4).
A lower bound for selection from a partial order, combining the weight of Fussenegger and Gabow [8] with its order-reversed counterpart (§ 5), with a formal proof in Lean 4 for all finite partial orders.
A search method that made the constructions practical (§ 6).
Statements that rest only on search runs, such as refutations under a fixed opening, are labeled as such and are not used in any theorem.
Preliminaries
A selection problem is a strict partial order on a set of elements, recording the comparisons known so far and their transitive consequences, together with a rank . Write for the minimum worst-case number of further comparisons. We write for the empty order on elements (the antichain), in which nothing is known. Since an algorithm starts in this state, by definition. The inputs are totally ordered, but the algorithm knows only . Two elements whose order is not yet known, that is, not implied by , are called incomparable, written ; only such pairs are worth comparing. denotes the transitive closure of with added. For an unsolved problem
and once some element is known to have exactly elements below and above it.
An element with at least known predecessors is too large to be the answer, and one with at least known successors is too small. Deleting such an element does not change the optimal cost [5]; deleting a too-small element lowers the rank by one. Problems that are isomorphic, or dual under order reversal with rank , have the same cost.
Transfer lemma [5 Lemma 1]. For ,
For the first inequality, run an algorithm for rank of elements on the given elements and an artificial element larger than all of them, answering comparisons with that element without cost. It returns the -th smallest given element. On every path the artificial element must be compared at least once, since it must end up known to lie above the answer, so every path saves at least one comparison. An artificial smallest element gives the second inequality.
Certificates and their verification
Certificates
We use the standard decision-tree model [12 Sec. 5.3]. For fixed , a deterministic algorithm that accesses the elements only through comparisons corresponds to a binary tree: each internal node compares two elements and branches on the outcome, and each leaf returns an element. An input follows a single root-to-leaf path, so the worst-case number of comparisons is the depth of the tree, and is the minimum depth of a tree that always returns the -th smallest element. An upper bound is therefore proved by exhibiting one such tree of depth at most ; a lower bound must hold for every such tree. Merging identical subtrees turns the tree into a directed acyclic graph. Our certificates are such graphs, written as Rust programs in the format of the search code of [5]. Each function solves a remaining subproblem: it may compare two arguments, discard arguments that cannot be the answer, and call another function on the remaining ones. A doc comment declares the size, the 0-based rank, and known relations. For example, this function from the 28-comparison certificate finds the median of three elements when is known:
/// n = 3, i = 1, b < c
fn select_12([a, b, c]: [usize; 3]) -> usize {
if a < b { select_1([b]) } else { select_2([a, c]) }
}
If , then and is returned. Otherwise is below both and , so it is discarded and the minimum of and is found with one more comparison. The root function select_0 receives all inputs with no known relations.
The static checker and why it is sound
The checker was written from the mathematical definitions without access to the search code, and accepts only a narrow grammar. It never uses the relations stated in comments. Instead it propagates knowledge from the root: each comparison outcome is added and closed transitively, and arguments are mapped to the callee’s names. A function called from several places gets as its precondition the relations that hold at every call site; this can only make acceptance harder. Under and each branch outcome it requires:
compared elements are incomparable before the comparison (this rejects wasted comparisons; soundness does not need it);
each call passes the declared number of pairwise distinct arguments;
each discarded element is certainly below the requested 0-based rank among elements (at least known successors) or certainly above it (at least known predecessors);
the callee’s declared rank equals minus the number of discarded elements below;
a directly returned element has exactly known predecessors and known successors.
It also checks that every function is reachable, the root is never called, and the call graph is acyclic.
Soundness. Call correct if for all distinct inputs whose order extends it terminates and returns the element of its declared rank. By induction in reverse topological order, which exists because the call graph is acyclic: an input of follows one checked branch; a returned element is correct by rule 5; for a call, the arguments are distinct inputs (rule 2) that satisfy the callee’s precondition, since that precondition was computed from what every caller knows; and rules 3 and 4 show that the callee’s answer is the caller’s answer. Termination follows from acyclicity. The root has the empty precondition, so the program is correct on all inputs.
Comparison bound. Each input follows a single path from the root to a return and makes only the comparisons on that path. The worst case is therefore the largest number of comparisons on any path, not the size of the graph. The checker computes it backward: a return costs 0, and a comparison costs one more than the larger cost of its two branches. Paths that no input can follow only make this bound larger, so it remains a valid upper bound.
Tests. The checker accepts all 70 optimal algorithms published with [5] with exactly their known costs, and rejected every wrong or ill-formed program among 12,046 generated mutants. An independently written exact oracle, following every path without intersecting preconditions, confirms each of our certificates and finds that its exact worst case equals the checker’s bound. Each certificate also passes compiled executions on pseudorandom permutations.
Formal proofs in Lean
A Lean 4 development formalizes the same argument. It uses only Lean’s core library, so every definition the theorems depend on is contained in our own short specification. It defines programs of this kind, their execution with a comparison counter, and the predicate SelectsWithin(Q,n,r,w): on every list of distinct natural numbers, program returns an input with exactly smaller inputs, using at most comparisons. Programs access inputs only by comparing them and passing them on, so they are comparison algorithms, and replacing distinct elements of any totally ordered set by their ranks leaves every execution unchanged. The development proves that acceptance by a checker implies the predicate, and the kernel evaluates that checker on the certificate data, giving
The proofs use only the standard axioms propext, Classical.choice and Quot.sound, not native_decide, and they replay in the independent kernel checker leanchecker. The bounds do not depend on the tool that translates the Rust certificates into Lean: the theorems concern the translated programs themselves, and each is a valid witness. Only the further claim that these programs are the archived Rust files depends on the translation. We cross-checked it against an independent parser and interpreter. The lower-bound theory of § 5 is formalized in a separate development described there.
The 28-comparison algorithm
The 329-function certificate opens with 15 comparisons that reach the same partial order on every input, followed by an irregular endgame of at most 13 comparisons. Calling the smaller element of a comparison its winner: compare eight disjoint pairs; compare the winners of pairs to form four four-element tournaments; then run a tournament among the four losers of these finals, the runner-ups. Each four-element group then has winner , its first opponent , runner-up , and the runner-up’s first opponent , with , , . Comparing the runner-ups of two such groups gives , and comparing the smaller runner-ups of two copies of gives (Figure 1). Swapping an outcome only renames components, so every input reaches after comparisons.

Figure 1. The opening of the 28-comparison algorithm for the 7th smallest of 16. (a) Eight pair comparisons, four group finals, and three comparisons among the runner-ups use 15 comparisons, leaving 13. Both outcomes at each step give the same known order after renaming. (b) The resulting order . Relations run upward from smaller to larger elements: an upward path gives a known inequality. Edges implied by such paths are omitted. In group , beat and then , while beat . Blue thick edges compare runner-ups, with and . (c) The next two comparisons. Each box shows (remaining elements, requested rank) and comparisons left; “drop” names newly discarded elements. The left branch is worked through in the text.
For example, the 16th comparison is against . If , then lies below the ten elements , , , , , , , , , , so it cannot be the 7th smallest; it is discarded, leaving the 6th smallest of 15 with 12 comparisons. We found no short set of rules describing the endgame. The second 28-comparison certificate and the 252-function 29-comparison certificate for the 8th smallest also pass through . The opening is not optimal for every rank: the bound of § 5 excludes ranks 1–3, and for the 8th smallest our search finds no 28-comparison completion (a search-level result), so is optimal there only if .
Lower bounds from a partially ordered state
This section bounds from below the number of comparisons that are still needed once some comparisons are known. Such bounds let the search discard states whose remaining budget is provably too small. We combine two classical weights into a new bound on the number of leaves of every correct decision tree (Theorem 5), recall a bound that follows a single input (Theorem 6), show that the combined weight is the best in a natural family (Proposition 8), and explain why no leaf count alone can settle (Remark 9).
A binary tree of depth has at most leaves, so a lower bound on the number of leaves of every correct tree gives .
A compatible solution is a triple with such that some total order extending lists , then , then ; let be their set. At a correct leaf the known relations determine the sets below and above the returned element : otherwise could be placed directly after its known predecessors in one consistent total order and directly before its known successors in another, giving different ranks. Hence distinct solutions need distinct leaves and [5 Thm. 6]. This is weak: finding the minimum of four elements has four solutions but needs three comparisons, because each non-minimum must lose once.
Weights. A down-set contains all known predecessors of its members; let be the down-sets of size . For , the possible answers are the minimal elements of ; let be their number minus one. For , the possible answers are the maximal elements of ; let be their number minus one. A solution corresponds to and , so . Define
Fussenegger and Gabow [8 Thm. 2(b)] give a weight of this kind for arbitrary partial orders (stated for the largest elements); after reversing the order, their weight is at most , with equality when no comparisons are known. The code of [5] computes . The new element is the combination . For the minimum of four elements, , , , and gives the correct bound 3.
The key step is to follow how a single comparison changes the weight of one fixed lower set . After the known order has grown from to , the set contributes
where is computed in . The lemma says that the weight of never shrinks in total over the two outcomes.
Lemma 4. Let and be incomparable in , let and be the two possible outcomes of comparing them, and fix .
If both and lie in , the weight is unchanged in both outcomes: .
If exactly one of them lies in , one outcome keeps the weight and the other sets it to 0.
If neither lies in , the two outcomes together keep at least the weight: .
The same holds for an initial set with weight , with inside and outside exchanged: the weight is unchanged if , one outcome keeps it if exactly one of them lies in , and the two outcomes together keep at least the weight if .
Proof. Adding creates only relations with and . Let . If , every lies in , so no relation inside and none from into is created; this gives the first case and the surviving outcome of the second. The outcome placing an element of below one of destroys as a down-set. If , then stays a down-set in both outcomes, and a minimal of can gain a predecessor in only through , which forces . So drops by at most one in each child, and . Order reversal gives the statement for .
Theorem 5. Every correct decision tree for has at least leaves. Hence .
Proof. Removing comparisons with known outcomes does not increase the number of leaves, and at a correct leaf the problem is solved, where and . It therefore suffices to show for incomparable , and to sum this inequality over the tree. Write . Every compatible solution survives in at least one child, the children’s solutions are solutions of , and a solution survives in both exactly when and lie on the same side of its answer. Hence
Down-sets of the children are down-sets of . By Lemma 4, every contributes at least 0 to , and with contributes exactly ; likewise for and with . Therefore
since .
Theorem 6. .
Proof. This is the classical argument about crucial comparisons [12, Sec. 5.3.3], [8], applied along one total order. Answer all comparisons according to a total order extending that lists , then , then ; let and . A new relation inside requires a comparison with both ends in , since is an initial segment of that order, and such a comparison removes at most one maximal element of . Dually, only comparisons inside remove minimal elements of , at most one each. At the end must be the only maximal element of and the only minimal element of , which needs at least and comparisons respectively. As , no comparison is counted twice.
Corollary 7. . Since gives , this is never weaker than the compatible-solution bound or either one-sided weight bound.
Within the family with , the choice gives . It is the largest valid member:
Proposition 8. If and for all , then for all , and hence on every problem.
Proof. For the minimum of there is one lower set with and singleton initial sets with , so . The maximum gives the bound for . Substituting termwise gives .
Formal verification. A second Lean 4 development, based on Mathlib, formalizes Lemma 4, Theorems 5 and 6, Corollary 7 and Proposition 8 for every finite strict partial order and every rank , with defined through total orders extending as above. Its decision trees may compare any two of the original elements, even when their order is known; a tree is correct if it returns the -th smallest element for every total order extending , and comparisons are counted along the path of each such input. Theorem 5 is proved for the number of leaves and as for every correct tree making at most comparisons on every input, with computed in the integers. The proofs use only the standard axioms and replay in leanchecker; the trusted base now includes the Mathlib definitions used in the statements. The evaluation and Remark 9 rest on computation and are not formalized.
Evaluation. At the empty order, improves the one-sided bound by one comparison for fifteen pairs with and , but gives only 23 for ; it does not reproduce the lower bound 28. The local inequality of Theorem 5 holds on all 149,598 cases with at most 7 elements (isomorphism classes, unsolved ranks, all incomparable pairs). An independent exhaustive check over all partial orders with at most 9 elements, up to isomorphism, found no violation of Theorems 5 and 6 against exact values. On a corpus of 1,663,508 exactly solved reduced problems with at most 12 elements, gathered by our exact solver during its searches, the average gap to the exact value is 2.62 for and 1.13 for LB, and LB is exact on 10% of them.
Remark 9 (A limit of leaf counting). No lower bound on the number of leaves of every correct tree can prove by taking its ceiling logarithm. The 252-function 29-comparison certificate unfolds into a tree with 248,381,440 syntactic leaves, fewer than , computed by two independent counts. Any bound valid for all correct trees is at most this number, so its ceiling logarithm is at most 28. A proof of 29, if true, must use depth or path information.
How the algorithms were found
The search is not part of the proofs; it only had to produce certificates. Three changes to the forward search of [5] made this practical.
Fixed budget, first success. That search computes optima by trying budgets upward from a lower bound and keeps looking for better comparisons after finding one that works. We instead fix a budget and accept the first comparison whose two outcomes are both solvable within . One or two comparisons of slack make construction much easier: for , with optimum 25, the time fell from 659 s at the optimum to 119 s and 17.6 s at one and two above it.
Information-greedy ordering. Candidate comparisons are tried in increasing order of the larger compatible-solution count of their two outcomes. On two benchmarks this reduced the searched nodes by factors of about 20 and 100, and at the optimum the time for fell from 659 s to 3.0 s. With this ordering, budget 29 for was solved in about two minutes and budget 29 for in 17 minutes, but budget 28 for remained unsolved after about 50 minutes. The bound LB of Corollary 7 serves as the pruning rule.
Proof-number search. Better ordering alone did not reach the tightest budgets. Even an ordering by , which put an optimal comparison first in about 96% of exactly solved problems with 7 to 10 elements, did not find a 26-comparison algorithm for by depth-first search. This suggested that the difficulty lay in revising early choices rather than in making them: depth-first search can spend very long refuting one early wrong choice before trying another. Construction is an AND/OR problem: one comparison must succeed at each state, and both of its outcomes must succeed. Depth-first proof-number search (df-pn) [1, 13, 11] redirects effort to cheaper alternatives before a choice is fully refuted. We initialize unexplored nodes from a cost predictor fitted to the corpus of solved small problems, shifted pessimistically, which mattered by factors of –. Table 4 compares the two searches; df-pn found the 28-comparison algorithm for in five seconds.
| instance | df-pn | depth-first | functions |
| at 25 | 0.55 s | 3.0 s | 412 |
| at 26 | 79 s | not found (35 min) | 1614 |
| at 28 | 5.0 s | not found (50 min) | 329 |
| at 29 | 320 s | 2 min | 2937 |
Table 4. Time to construct an algorithm at the stated budget (single thread).
For 17 elements we widened the solver’s representation to at most 19 elements; it reproduces earlier certificates byte for byte. Starting from the upper bounds of [9], df-pn reached the bounds of Theorem 3 within 89 s to 63 min per rank (Table 3); for rank 5 this meets the lower bound.
| Bound | Functions | Search time | SHA-256 prefix |
| 329 | 5 s | eb8f064c12bb | |
| 2937 | 5 min | 2e9615ae1d9b | |
| 146 | 103 s | 34e26f201b68 | |
| 266 | 89 s | ae6d6b42e1c4 | |
| 977 | 8 min | 33718071f64a | |
| 1077 | 19 min | 5925b96967a8 | |
| 1493 | 63 min | 9ce649f22b2d |
Table 3. The certificates used in the Lean proofs, found by proof-number search (single thread, AMD Ryzen 7 5800X). Further certificates for the 16-element bounds, with 365 and 252 functions, are archived.
For at 28, two df-pn runs of 6,600 s and a six-hour portfolio ended undecided. Refutations one below the optimum grew by factors of about 100–300 per added element on the instances we measured, so an exhaustive refutation at 16 elements was out of reach on our desktop.
Conclusion and open problems
The previous best upper bounds for these entries came from a search restricted to a few heuristically chosen comparisons per node [9]; the exhaustive search of [5] aims at optimal values, which were out of reach for them. Our results indicate that much of the remaining gap was a matter of search strategy. Asking only for some algorithm within a fixed budget avoids computing optimal values of subproblems, and ordering comparisons by the remaining uncertainty makes good choices likely. Near the optimum this was not enough: the difficulty appeared to lie in revising early choices, and proof-number search, which can turn to an alternative before the current choice is fully refuted, overcame it. On some instances, such as the median of 16 at budget 29, depth-first search was faster, so the two strategies are complementary. Since every bound is checked independently of the search, none of these heuristics needs to be correct for the results to hold.
The same experience shows where the difficulty now lies. Constructing an algorithm became cheap, but proving that none exists within a budget remained expensive: refutations one below the optimum grew by factors of about 100–300 per added element in our measurements, and the 28-comparison question for the median of 16 stayed undecided. For the open entries, progress depends mainly on lower bounds.
Open problems. Is equal to 28 or 29? A 28-comparison certificate would settle it constructively; a proof of 29 must exclude all such algorithms and, by Remark 9, needs more than a total leaf count. An analytic proof of would remove the dependence on a large computation. The gaps for at ranks 6–9 remain open. Finally, can the runner-up opening be extended into a family of good selection algorithms, for example for central ranks of elements?
Data availability
Code, certificates, and Lean proofs are archived on Zenodo as release v1.0.0 [16]. The development repository is available at https://github.com/JCremerr/selection-7-of-16.
Use of AI
This work was inspired by recent AI-assisted results in combinatorics [4] and was carried out with AI coding assistants. The author chose the problem, directed the searches, and developed the text with the assistants in successive revisions. The assistants implemented the software and produced first drafts of the proofs. The author is responsible for the content.
Acknowledgments
We thank J. Dörrer, K. Gendle, J. Betz, J. von Smercek, A. Steding and F. Stober for publishing their search code and optimal algorithms.
References
- [1]L. V. Allis, M. van der Meulen, and H. J. van den Herik. Proof-number search. Artificial Intelligence, 66(1):91–124, 1994.
- [2]M. Blum, R. W. Floyd, V. Pratt, R. L. Rivest, and R. E. Tarjan. Time bounds for selection. Journal of Computer and System Sciences, 7(4):448–461, 1973.DOI
- [3]S. W. Bent and J. W. John. Finding the median requires 2n comparisons. In Proc. 17th ACM Symposium on Theory of Computing, pages 213–216, 1985.DOI
- [4]F. Cremer. Lower bounds for Ramsey numbers: R(6, 8) ≥ 135 and R(8, 10) ≥ 345. arXiv:2610.12122, 2026.arxiv.org/abs/2610.12122
- [5]J. Dörrer, K. Gendle, J. Betz, J. von Smercek, A. Steding, and F. Stober. Exact lower bounds for the number of comparisons in selection. In 23rd International Symposium on Experimental Algorithms (SEA 2025), LIPICs 338, pages 16:1–16:15, 2025. doi:10.4230/LIPICs.SEA.2025.16.
- [6]D. Dor and U. Zwick. Selecting the median. SIAM Journal on Computing, 28(5):1722–1758, 1999.
- [7]D. Dor and U. Zwick. Median selection requires (2 + ε)n comparisons. SIAM Journal on Discrete Mathematics, 14(3):312–325, 2001.
- [8]F. Fussenegger and H. N. Gabow. A counting approach to lower bounds for selection problems. Journal of the ACM, 26(2):227–238, 1979.DOI
- [9]W. Gasarch, W. Kelly, and W. Pugh. Finding the ith largest of n for small i, n. SIGACT News, 27(2):88–96, 1996. doi:10.1145/235767.235772.
- [10]A. Hadian and M. Sobel. Selecting the t-th largest using binary errorless comparisons. Technical report, University of Minnesota, 1969.
- [11]A. Kishimoto, M. H. M. Winands, M. Müller, and J.-T. Saito. Game-tree search using proof numbers: The first twenty years. ICGA Journal, 35(3):131–156, 2012.DOI
- [12]D. E. Knuth. The Art of Computer Programming, Vol. 3: Sorting and Searching. Addison-Wesley, 2nd edition, 1998.
- [13]A. Nagai. Df-pn Algorithm for Searching AND/OR Trees and Its Applications. PhD thesis, University of Tokyo, 2002.
- [14]K. Oksanen. Selecting the ith largest of n elements. https://www.cs.hut.fi/~cessu/selection/, 2002.
- [15]K. Oksanen. Searching for selection algorithms. Electronic Notes in Discrete Mathematics, 27:77, 2006.DOI
- [16]J. Cremer. Selecting the 7th Smallest of 16 Elements Takes Exactly 28 Comparisons: code, certificates and Lean proofs. Version v1.0.0. Zenodo, 2026. doi:10.5281/zenodo.23346948.DOI