format: text

text:
# Randomized selection revisitedResearch supported by the State Committee for Scientific Research under Grant 8T11A00622.

## 1 Introduction

The selection problem is defined as follows: Given a set $`X:=\{x_j\}_{j=1}^n`$ of $`n`$ elements, a total order $`<`$ on $`X`$, and an integer $`1kn`$, find the $`k`$th smallest element of $`X`$, i.e., an element $`x`$ of $`X`$ for which there are at most $`k1`$ elements $`x_j<x`$ and at least $`k`$ elements $`x_jx`$. The median of $`X`$ is the $`n/2`$th smallest element of $`X`$. (Since we are not assuming that the elements are distinct, $`X`$ may be regarded as a multiset).

Selection is one of the fundamental problems in computer science. It is used in the solution of other basic problems such as sorting and finding convex hulls. Hence its literature is too vast to be reviewed here; see, e.g., \[DHUZ01, DoZ99, DoZ01\] and \[Knu98, §5.3.3\]. We only stress that most references employ a comparison model (in which a selection algorithm is charged only for comparisons between pairs of elements), assuming that the elements are distinct. Then, in the worst case, selection needs at least $`(2+ϵ)n`$ comparisons \[DoZ01\], whereas the pioneering algorithm of \[BFP+72\] makes at most $`5.43n`$, its first improvement of \[SPP76\] needs $`3n+o(n)`$, and the most recent improvement in \[DoZ99\] takes $`2.95n+o(n)`$. Thus a gap of almost $`50\%`$ still remains between the best lower and upper bounds in the worst case.

The average case is better understood. Specifically, for $`kn/2`$, at least $`n+k2`$ comparisons are necessary \[CuM89\], \[Knu98, Ex. 5.3.3–25\], whereas the best upper bound is $`n+k+O(n^{1/2}\mathrm{ln}^{1/2}n)`$ \[Knu98, Eq. (5.3.3.16)\]. Yet this bound holds for a hardly implementable theoretical scheme \[Knu98, Ex. 5.3.3–24\], whereas a similar frequently cited bound for the algorithm Select of \[FlR75b\] doesn’t have a full proof, as noted in \[Knu98, Ex. 5.3.3–24\] and \[PRKT83\]. Significantly worse bounds hold for the classical algorithm Find of \[Hoa61\], also known as quickselect, which partitions $`X`$ by using the median of a random sample of size $`s1`$. In particular, for $`k=n/2`$, the upper bound is $`3.39n+o(n)`$ for $`s=1`$ \[Knu98, Ex. 5.2.2–32\] and $`2.75n+o(n)`$ for $`s=3`$ \[Grü99, KMP97\], whereas for finding an element of random rank, the average cost is $`3n+o(n)`$ for $`s=1`$, $`2.5n+o(n)`$ for $`s=3`$ \[KMP97\], and $`2n+o(n)`$ when $`s\mathrm{}`$, $`s/n0`$ as $`n\mathrm{}`$ \[MaR01\]. In practice Find is most popular, because the algorithms of \[BFP+72, SPP76\] are much slower on the average \[Mus97, Val00\]. For the general case of nondistinct elements, little is known in theory about these algorithms, but again Find performs well in practice \[Val00\].

Our aim is to rekindle theoretical and practical interest in the algorithm Select of \[FlR75b, §2.1\] (the versions of \[FlR75b, §2.3\] and \[FlR75a\] will be addressed elsewhere). We show that Select performs very well in both theory and practice, even when equal elements occur. To outline our contributions in more detail, we recall that Select operates as follows. Using a small random sample, two elements $`u`$ and $`v`$ almost sure to be just below and above the $`k`$th are found. The remaining elements are compared with $`u`$ and $`v`$ to create a small selection problem on the elements between $`u`$ and $`v`$ that is quickly solved recursively. By taking a random subset as the sample, this approach does well against any input ordering, both on average and with high probability.

First, we revise Select slightly to simplify our analysis. Then, without assuming that the elements are distinct, we show that Select needs at most $`n+\mathrm{min}\{k,nk\}+O(n^{2/3}\mathrm{ln}^{1/3}n)`$ comparisons on average; this agrees with the result of \[FlR75b, §2.2\] which is based on an unproven assumption \[PRKT83, §5\]. Similar upper bounds are established for versions that choose sample sizes as in \[FlR75a, Meh00, Rei85\] and \[MoR95, §3.3\]. Thus the average costs of these versions reach the lower bounds of $`1.5n+o(n)`$ for median selection and $`1.25n+o(n)`$ for selecting an element of random rank (yet the original sample size of \[FlR75b, §2.2\] has the best lower order term in its cost). We also prove that nonrecursive versions of Select, which employ other selection or sorting algorithms for small subproblems, require at most $`n+\mathrm{min}\{k,nk\}+o(n)`$ comparisons with high probability (e.g., $`14n^{2\beta }`$ for a user-specified $`\beta >0`$); this extends and strengthens the results of \[GeS96, Thm 2\], \[Meh00, Thm 2\] and \[MoR95, Thm 3.5\].

Since theoretical bounds alone needn’t convince practitioners (who may worry about hidden constants, etc.), a serious effort was made to design a competitive implementation of Select. Here, as with Find and quicksort \[Sed77\], the partitioning efficiency is crucial. In contrast with the observation of \[FlR75b, p. 169\] that “partitioning $`X`$ about both $`u`$ and $`v`$ \[is\] an inherently inefficient operation”, we introduce several quintary schemes which perform well in practice. As a byproduct, we give a modification of the ternary partitioning scheme of \[BeM93, BeS97\] that obviates subscript range checking.

Relative to Find, Select requires only small additional stack space for recursion, because sampling without replacement can be done in place. Still, it might seem that random sampling needs too much time for random number generation. (Hence several popular implementations of Find don’t sample randomly, assuming that the input file is in random order, whereas others \[Val00\] invoke random sampling only when slow progress occurs.) Yet our computational experience shows that sampling doesn’t hurt even on random inputs, and it helps a lot on more difficult inputs (in fact our interest in Select was sparked by the poor performance of the implementation of \[FlR75a\] on several inputs of \[Val00\]). Most importantly, even for examples with relatively low comparison costs, Select beats quite sophisticated implementations of Find by a wide margin, in both comparison counts and computing times. To save space, only selected results are reported, but our experience on many other inputs was similar. In particular, empirical estimates of the constants hidden in our bounds were always quite small. Further, the performance of Select is extremely stable across a variety of inputs, even for small input sizes (cf. §7.3). A theoretical explanation of these features will be undertaken elsewhere. For now, our experience supports the claim of \[FlR75b, §1\] that “the algorithm presented here is probably the best practical choice”.

The paper is organized as follows. A general version of Select is introduced in §2, and its basic features are analyzed in §3. The average performance of Select is studied in §4. High probability bounds for nonrecursive versions are derived in §5. Partitioning schemes are discussed in §6. Finally, our computational results are reported in §7.

Our notation is fairly standard. $`|A|`$ denotes the cardinality of a set $`A`$. In a given probability space, $`\mathrm{P}`$ is the probability measure, and $`\mathrm{E}`$ is the mean-value operator.

## 2 The algorithm Select

In this section we describe a general version of Select in terms of two auxiliary functions $`s(n)`$ and $`g(n)`$ (the sample size and rank gap), which will be chosen later. We omit their arguments in general, as no confusion can arise.

Select picks a small random sample $`S`$ from $`X`$ and two pivots $`u`$ and $`v`$ from $`S`$ such that $`ux_k^{}v`$ with high probability, where $`x_k^{}`$ is the $`k`$th smallest element of $`X`$. Partitioning $`X`$ into elements less than $`u`$, between $`u`$ and $`v`$, greater than $`v`$, and equal to $`u`$ or $`v`$, Select either detects that $`u`$ or $`v`$ equals $`x_k^{}`$, or determines a subset $`\widehat{X}`$ of $`X`$ and an integer $`\widehat{k}`$ such that $`x_k^{}`$ may be selected recursively as the $`\widehat{k}`$th smallest element of $`\widehat{X}`$.

Below is a detailed description of the algorithm.

###### Algorithm 2.1

Select$`(X,k)`$ (Selects the $`k`$th smallest element of $`X`$, with $`1kn:=|X|`$)

Step 1 (Initiation). If $`n=1`$, return $`x_1`$. Choose the sample size $`sn1`$ and gap $`g>0`$.

Step 2 (Sample selection). Pick randomly a sample $`S:=\{y_1,\mathrm{},y_s\}`$ from $`X`$.

Step 3 (Pivot selection). Set $`i_u:=\mathrm{max}\{ks/ng,1\}`$, $`i_v:=\mathrm{min}\{ks/n+g,s\}`$. Let $`u`$ and $`v`$ be the $`i_u`$th and $`i_v`$th smallest elements of $`S`$, found by using Select recursively.

Step 4 (Partitioning). By comparing each element $`x`$ of $`X`$ to $`u`$ and $`v`$, partition $`X`$ into $`A:=\{xX:x<u\}`$, $`B:=\{xX:x=u\}`$, $`C:=\{xX:u<x<v\}`$, $`D:=\{xX:x=v\}`$, $`E:=\{xX:v<x\}`$. If $`k<n/2`$, $`x`$ is compared to $`v`$ first, and to $`u`$ only if $`x<v`$ and $`u<v`$. If $`kn/2`$, the order of the comparisons is reversed.

Step 5 (Stopping test). If $`|A|<k|AB|`$ then return $`u`$; else if $`|ABC|<kn|E|`$ then return $`v`$.

Step 6 (Reduction). If $`k|A|`$, set $`\widehat{X}:=A`$ and $`\widehat{k}:=k`$; else if $`n|E|<k`$, set $`\widehat{X}:=E`$ and $`\widehat{k}:=kn+|E|`$; else set $`\widehat{X}:=C`$ and $`\widehat{k}:=k|AB|`$. Set $`\widehat{n}:=|\widehat{X}|`$.

Step 7 (Recursion). Return Select$`(\widehat{X},\widehat{k})`$.

A few remarks on the algorithm are in order.

###### Remarks 2.2

(a) The correctness and finiteness of Select stem by induction from the following observations. The returns of Steps 1 and 5 deliver the desired element. At Step 6, $`\widehat{X}`$ and $`\widehat{k}`$ are chosen so that the $`k`$th smallest element of $`X`$ is the $`\widehat{k}`$th smallest element of $`\widehat{X}`$, and $`\widehat{n}<n`$ (since $`u,v\widehat{X}`$). Also $`|S|<n`$ for the recursive calls at Step 3.

(b) When Step 5 returns $`u`$ (or $`v`$), Select may also return information about the positions of the elements of $`X`$ relative to $`u`$ (or $`v`$). For instance, if $`X`$ is stored as an array, its $`k`$ smallest elements may be placed first via interchanges at Step 4 (cf. §6). Hence after Step 3 finds $`u`$, we may remove from $`S`$ its first $`i_u`$ smallest elements before extracting $`v`$. Further, Step 4 need only compare $`u`$ and $`v`$ with the elements of $`XS`$.

(c) The following elementary property is needed in §4. Let $`c_n`$ denote the maximum number of comparisons taken by Select on any input of size $`n`$. Since Step 3 makes at most $`c_s+c_{si_u}`$ comparisons with $`s<n`$, Step 4 needs at most $`2(ns)`$, and Step 7 takes at most $`c_{\widehat{n}}`$ with $`\widehat{n}<n`$, by induction $`c_n<\mathrm{}`$ for all $`n`$.

## 3 Preliminary analysis

In this section we analyze general features of sampling used by Select.

### 3.1 Sampling deviations and expectation bounds

Our analysis hinges on the following bound on the tail of the hypergeometric distribution established in \[Hoe63\] and rederived shortly in \[Chv79\].

###### Fact 3.1

Let $`s`$ balls be chosen uniformly at random from a set of $`n`$ balls, of which $`r`$ are red, and $`r^{}`$ be the random variable representing the number of red balls drawn. Let $`p:=r/n`$. Then

$$\mathrm{P}\left[r^{}ps+g\right]e^{2g^2/s}g0.$$
(3.1)
We shall also need a simple version of the (left) Chebyshev inequality \[Kor78, §2.4.2\].

###### Fact 3.2

Let $`z`$ be a nonnegative random variable such that $`\mathrm{P}[z\zeta ]=1`$ for some constant $`\zeta `$. Then $`\mathrm{E}zt+\zeta \mathrm{P}[z>t]`$ for all nonnegative real numbers $`t`$.

### 3.2 Sample ranks and partitioning efficiency

Denote by $`x_1^{}\mathrm{}x_n^{}`$ and $`y_1^{}\mathrm{}y_s^{}`$ the sorted elements of the input set $`X`$ and the sample set $`S`$, respectively. Thus $`x_k^{}`$ is the $`k`$th smallest element of $`X`$, whereas $`u=y_{i_u}^{}`$ and $`v=y_{i_v}^{}`$ at Step 3. This notation facilitates showing that for the bounding indices

$$k_l:=\mathrm{max}\{k2gn/s,1\}\text{and}k_r:=\mathrm{min}\{k+2gn/s,n\},$$
(3.2)
we have $`x_{k_l}^{}ux_k^{}vx_{k_r}^{}`$ with high probability for suitable choices of $`s`$ and $`g`$.

###### Lemma 3.3

(a) $`\mathrm{P}[x_k^{}<u]e^{2g^2/s}`$ if $`i_u=ks/ng`$.

(b)(a) $`\mathrm{P}[u<x_{k_l}^{}]e^{2g^2/s}`$.

(c)(a) $`\mathrm{P}[v<x_k^{}]e^{2g^2/s}`$ if $`i_v=ks/n+g`$.

(d)(a) $`\mathrm{P}[x_{k_r}^{}<v]e^{2g^2/s}`$.

(e)(a) $`i_uks/ng`$ iff $`kgn/s`$; $`i_vks/n+g`$ iff $`n<k+gn/s`$.

* Proof. (a) If $`x_k^{}<y_{i_u}^{}`$, at least $`si_u+1`$ samples satisfy $`y_ix_{\overline{ȷ}+1}^{}`$ with $`\overline{ȷ}:=\mathrm{max}_{x_j^{}=x_k^{}}j`$. In the setting of Fact 3.1, we have $`r:=n\overline{ȷ}`$ red elements $`x_jx_{\overline{ȷ}+1}^{}`$, $`ps=s\overline{ȷ}s/n`$ and $`r^{}si_u+1`$. Since $`i_u=ks/ng<ks/ng+1`$ and $`\overline{ȷ}k`$, we get $`r^{}>ps+(\overline{ȷ}k)s/n+gps+g`$. Hence $`\mathrm{P}[x_k^{}<u]\mathrm{P}[r^{}ps+g]e^{2g^2/s}`$ by (3.1).

(b) If $`y_{i_u}^{}<x_{k_l}^{}`$, $`i_u`$ samples are at most $`x_r^{}`$, where $`r:=\mathrm{max}_{x_j^{}<x_{k_l}^{}}j`$. Thus we have $`r`$ red elements $`x_jx_r^{}`$, $`ps=rs/n`$ and $`r^{}i_u`$. Now, $`1rk_l1`$ implies $`2k_l=k2gn/s`$ by (3.2) and thus $`k_l<k2gn/s+1`$, so $`rs/n>ks/n+2g`$. Hence $`i_upsgks/ngrs/ng>0`$, i.e., $`r^{}>ps+g`$; invoke (3.1) as before.

(c) If $`y_{i_v}^{}<x_k^{}`$, $`i_v`$ samples are at most $`x_r^{}`$, where $`r:=\mathrm{max}_{x_j^{}<x_k^{}}j`$. Thus we have $`r`$ red elements $`x_jx_r^{}`$, $`ps=rs/n`$ and $`r^{}i_v`$. But $`i_vpsgks/n+grs/ng0`$ implies $`r^{}ps+g`$, so again (3.1) yields the conclusion.

(d) If $`x_{k_r}^{}<y_{i_v}^{}`$, $`si_v+1`$ samples are at least $`x_{\overline{ȷ}+1}^{}`$, where $`\overline{ȷ}:=\mathrm{max}_{x_j^{}=x_{k_r}^{}}j`$. Thus we have $`r:=n\overline{ȷ}`$ red elements $`x_jx_{\overline{ȷ}+1}^{}`$, $`ps=s\overline{ȷ}s/n`$ and $`r^{}si_v+1`$. Now, $`i_v<ks/n+g+1`$ and $`\overline{ȷ}k_rk+2gn/s`$ (cf. (3.2)) yield $`si_v+1psg\overline{ȷ}s/nks/ng1+1g0`$. Thus $`x_{k_r}^{}<v`$ implies $`r^{}ps+g`$; hence $`\mathrm{P}[x_{k_r}^{}<v]\mathrm{P}[r^{}ps+g]e^{2g^2/s}`$ by (3.1).

(e) Follows immediately from the properties of $``$ \[Knu97, §1.2.4\].        A

We may now estimate the partitioning costs of Step 4. We assume that only necessary comparisons are made (but it will be seen that up to $`s`$ extraneous comparisons may be accomodated in our analysis; cf. Rem. 5.4(a)).

###### Lemma 3.4

Let $`c`$ denote the number of comparisons made at Step $`4`$. Then

$$\mathrm{P}[c\overline{c}]1e^{2g^2/s}\text{and}\mathrm{E}c\overline{c}+2(ns)e^{2g^2/s}\text{with}$$
(3.3a)
$$\overline{c}:=n+\mathrm{min}\{k,nk\}s+2gn/s.$$
(3.3b)
* Proof. Consider the event $`𝒜:=\{c\overline{c}\}`$ and its complement $`𝒜^{}:=\{c>\overline{c}\}`$. If $`u=v`$ then $`c=ns\overline{c}`$; hence $`\mathrm{P}[𝒜^{}]=\mathrm{P}[𝒜^{}\{u<v\}]`$, and we may assume $`u<v`$ below.

First, suppose $`k<n/2`$. Then $`c=ns+|\{xXS:x<v\}|`$, since $`ns`$ elements of $`XS`$ are compared to $`v`$ first. In particular, $`c2(ns)`$. Since $`k<n/2`$, $`\overline{c}=n+ks+2gn/s`$. If $`vx_{k_r}^{}`$, then $`\{xXS:x<v\}\{xX:xv\}\{u,v\}`$ yields $`|\{xXS:x<v\}|k_r2`$, so $`cns+k_r2`$; since $`k_r<k+2gn/s+1`$, we get $`cn+ks+2gn/s1\overline{c}`$. Thus $`u<vx_{k_r}^{}`$ implies $`𝒜`$. Therefore, $`𝒜^{}\{u<v\}`$ implies $`\{x_{k_r}^{}<v\}\{u<v\}`$, so $`\mathrm{P}[𝒜^{}\{u<v\}]\mathrm{P}[x_{k_r}^{}<v]e^{2g^2/s}`$ (Lem. 3.3(d)). Hence we have (3.4), since $`\mathrm{E}c\overline{c}+2(ns)e^{2g^2/s}`$ by Fact 3.2 (with $`z:=c`$, $`\zeta :=2(ns)`$).

Next, suppose $`kn/2`$. Now $`c=ns+|\{xXS:u<x\}|`$, since $`ns`$ elements of $`XS`$ are compared to $`u`$ first. If $`x_{k_l}^{}u`$, then $`\{xXS:u<x\}\{xX:ux\}\{u,v\}`$ yields $`|\{xXS:u<x\}|nk_l1`$; hence $`k_lk2gn/s`$ gives $`cns+(nk)+2gn/s1\overline{c}`$. Thus $`𝒜^{}\{u<v\}`$ implies $`\{u<x_{l_r}^{}\}\{u<v\}`$, so $`\mathrm{P}[𝒜^{}\{u<v\}]\mathrm{P}[u<x_{k_l}^{}]e^{2g^2/s}`$ (Lem. 3.3(b)), and we get (3.4) as before.        A

The following result will imply that, for suitable choices of $`s`$ and $`g`$, the set $`\widehat{X}`$ selected at Step 6 will be “small enough” with high probability and in expectation; we let $`\widehat{X}:=\mathrm{}`$ and $`\widehat{n}:=0`$ if Step 5 returns $`u`$ or $`v`$, but we don’t consider this case explicitly.

###### Lemma 3.5

$`\mathrm{P}\left[\widehat{n}<4gn/s\right]14e^{2g^2/s}`$, and $`\mathrm{E}\widehat{n}4gn/s+4ne^{2g^2/s}`$.

* Proof. The first bound yields the second one by Fact 3.2 (with $`z:=\widehat{n}<n`$). In each case below, we define an event $``$ that implies the event $`:=\{\widehat{n}<4gn/s\}`$.

First, consider the middle case of $`i_u=ks/ng`$ and $`i_v=ks/n+g`$. Let $`:=\{x_{k_l}^{}ux_k^{}vx_{k_r}^{}\}`$. By Lem. 3.3 and the Boole-Benferroni inequality, its complement $`^{}`$ has $`\mathrm{P}[^{}]4e^{2g^2/s}`$, so $`\mathrm{P}[]14e^{2g^2/s}`$. By the rules of Steps 4–6, $`ux_k^{}v`$ implies $`\widehat{X}=C`$, whereas $`x_{k_l}^{}uvx_{k_r}^{}`$ yields $`\widehat{n}k_rk_l+12`$; since $`k_r<k+2gn/s+1`$ and $`k_lk2gn/s`$ by (3.2), we get $`\widehat{n}<4gn/s`$. Hence $``$ and thus $`\mathrm{P}[]\mathrm{P}[]`$.

Next, consider the left case of $`i_uks/ng`$, i.e., $`kgn/s`$ (Lem. 3.3(e)). If $`i_vks/n+g`$, then $`n<k+gn/s`$ (Lem. 3.3(e)) gives $`\widehat{n}<n<k+gn/s2gn/s`$; take $`:=\{n<k+gn/s\}`$, a certain event. For $`i_v=ks/n+g`$, let $`:=\{x_k^{}vx_{k_r}^{}\}`$; again $`\mathrm{P}[]12e^{2g^2/s}`$ by Lem. 3.3(c,d). Now, $`x_k^{}v`$ implies $`\widehat{X}AC`$, whereas $`vx_{k_r}^{}`$ gives $`\widehat{n}k_r1<k+2gn/s3gn/s`$; therefore $``$.

Finally, consider the right case of $`i_vks/n+g`$, i.e., $`n<k+gn/s`$. If $`i_uks/ng`$ then $`kgn/s`$ gives $`\widehat{n}<n<2gn/s`$; take $`:=\{kgn/s\}`$. For $`i_u=ks/ng`$, $`:=\{x_{k_l}^{}ux_k^{}\}`$ has $`\mathrm{P}[]12e^{2g^2/s}`$ by Lem. 3.3(a,b). Now, $`ux_k^{}`$ implies $`\widehat{X}CE`$, whereas $`x_{k_l}^{}u`$ yields $`\widehat{n}nk_l`$ with $`k_lk2gn/s`$ and thus $`\widehat{n}<3gn/s`$. Hence $``$.        A

###### Corollary 3.6

$`\mathrm{P}\left[c\overline{c}\text{and}\widehat{n}<4gn/s\right]14e^{2g^2/s}`$.

* Proof. Check that $``$ implies $`𝒜`$ in the proofs of Lems. 3.4 and 3.5; note that $`n2gn/s`$ yields $`c2(ns)\overline{c}`$ (cf. (3.3b)) in the left and right subcases.        A

###### Remark 3.7

Suppose Step 3 resets $`i_u:=i_v`$ if $`kgn/s`$, or $`i_v:=i_u`$ if $`n<k+gn/s`$, finding a single pivot $`u=v`$ in these cases. The preceding results remain valid.

## 4 Analysis of the recursive version

In this section we analyze the average performance of Select for various sample sizes.

### 4.1 Floyd-Rivest’s samples

For positive constants $`\alpha `$ and $`\beta `$, consider choosing $`s=s(n)`$ and $`g=g(n)`$ as

$$s:=\mathrm{min}\{\alpha f(n),n1\}\text{and}g:=(\beta s\mathrm{ln}n)^{1/2}\text{with}f(n):=n^{2/3}\mathrm{ln}^{1/3}n.$$
(4.1)
This form of $`g`$ gives a probability bound $`e^{2g^2/s}=n^{2\beta }`$ for Lems. 3.43.5. To get more feeling, suppose $`\alpha =\beta =1`$ and $`s=f(n)`$. Let $`\varphi (n):=f(n)/n`$. Then $`s/n=g/s=\varphi (n)`$ and $`\widehat{n}/n`$ is at most $`4\varphi (n)`$ with high probability (at least $`14/n^2`$), i.e., $`\varphi (n)`$ is a contraction factor; note that $`\varphi (n)2.4\%`$ for $`n=10^6`$ (cf. Tab. 4.1).

###### Theorem 4.1

Let $`C_{nk}`$ denote the expected number of comparisons made by Select for $`s`$ and $`g`$ chosen as in (4.1) with $`\beta 1/6`$. There exists a positive constant $`\gamma `$ such that

$$C_{nk}n+\mathrm{min}\{k,nk\}+\gamma f(n)1kn.$$
(4.2)
* Proof. We need a few preliminary facts. The function $`\varphi (t):=f(t)/t=(\mathrm{ln}t/t)^{1/3}`$ decreases to $`0`$ on $`[e,\mathrm{})`$, whereas $`f(t)`$ grows to infinity on $`[2,\mathrm{})`$. Let $`\delta :=4(\beta /\alpha )^{1/2}`$. Pick $`\overline{n}3`$ large enough so that $`e1\alpha f(\overline{n})\overline{n}1`$ and $`e\delta f(\overline{n})`$. Let $`\overline{\alpha }:=\alpha +1/f(\overline{n})`$. Then, by (4.1) and the monotonicity of $`f`$ and $`\varphi `$, we have for $`n\overline{n}`$

$$s\overline{\alpha }f(n)\text{and}f(s)\overline{\alpha }\varphi (\overline{\alpha }f(\overline{n}))f(n),$$
(4.3)
$$f(\delta f(n))\delta \varphi (\delta f(\overline{n}))f(n).$$
(4.4)
For instance, the first inequality of (4.3) yields $`f(s)f(\overline{\alpha }f(n))`$, whereas

$$f(\overline{\alpha }f(n))=\overline{\alpha }\varphi (\overline{\alpha }f(n))f(n)\overline{\alpha }\varphi (\overline{\alpha }f(\overline{n}))f(n).$$

Also for $`n\overline{n}`$, we have $`s=\alpha f(n)=\alpha f(n)+ϵ`$ with $`ϵ[0,1)`$ in (4.1). Writing $`s=\stackrel{~}{\alpha }f(n)`$ with $`\stackrel{~}{\alpha }:=\alpha +ϵ/f(n)[\alpha ,\overline{\alpha })`$, we deduce from (4.1) that

$$gn/s=(\beta /\stackrel{~}{\alpha })^{1/2}f(n)(\beta /\alpha )^{1/2}f(n).$$
(4.5)
In particular, $`4gn/s\delta f(n)`$, since $`\delta :=4(\beta /\alpha )^{1/2}`$. For $`\beta 1/6`$, (4.1) implies

$$ne^{2g^2/s}n^{12\beta }=f(n)n^{1/32\beta }\mathrm{ln}^{1/3}nf(n)\mathrm{ln}^{1/3}n.$$
(4.6)
Using the monotonicity of $`\varphi `$ and $`f`$ on $`[e,\mathrm{})`$, increase $`\overline{n}`$ if necessary to get

$$2\overline{\alpha }\varphi (\overline{\alpha }f(\overline{n}))+\delta \varphi (\delta f(\overline{n}))+4\varphi (\overline{n})\overline{n}^{1/32\beta }\mathrm{ln}^{1/3}\overline{n}0.95.$$
(4.7)
By Rem. 2.2(c), there is $`\gamma `$ such that (4.2) holds for all $`n\overline{n}`$; increasing $`\gamma `$ if necessary, we have

$$2\overline{\alpha }+2\delta +8\overline{n}^{1/32\beta }\mathrm{ln}^{1/3}\overline{n}0.05\gamma .$$
(4.8)
Let $`n^{}\overline{n}`$. Assuming (4.2) holds for all $`nn^{}`$, for induction let $`n=n^{}+1`$.

The cost of Step 3 can be estimated as follows. We may first apply Select recursively to $`S`$ to find $`u=y_{i_u}^{}`$, and then extract $`v=y_{i_v}^{}`$ from the elements $`y_{i_u+1}^{},\mathrm{},y_s^{}`$ (assuming $`i_u<i_v`$; otherwise $`v=u`$). Since $`sn^{}`$, the expected number of comparisons is

$$C_{si_u}+C_{si_u,i_vi_u}1.5s+\gamma f(s)+1.5(si_u)+\gamma f(si_u)3s1.5+2\gamma f(s).$$
(4.9)
The partitioning cost of Step 4 is estimated by (3.4) as

$$\mathrm{E}cn+\mathrm{min}\{k,nk\}s+2gn/s+2ne^{2g^2/s}.$$
(4.10)
The cost of finishing up at Step 7 is at most $`C_{\widehat{n}\widehat{k}}1.5\widehat{n}+\gamma f(\widehat{n})`$. But by Lem. 3.5, $`\mathrm{P}[\widehat{n}4gn/s]4e^{2g^2/s}`$, and $`\widehat{n}<n`$, so (cf. Fact 3.2 with $`z:=1.5\widehat{n}+\gamma f(\widehat{n})`$)

$$\mathrm{E}\left[\mathrm{\hspace{0.17em}1.5}\widehat{n}+\gamma f(\widehat{n})\right]1.54gn/s+\gamma f(4gn/s)+\left[\mathrm{\hspace{0.17em}1.5}n+\gamma f(n)\right]4e^{2g^2/s}.$$

Since $`4gn/s\delta f(n)`$, $`f`$ is increasing, and $`f(n)=\varphi (n)n`$ above, we get

$$\mathrm{E}C_{\widehat{n}\widehat{k}}6gn/s+\gamma f(\delta f(n))+\left[\mathrm{\hspace{0.17em}1.5}+\gamma \varphi (n)\right]4ne^{2g^2/s}.$$
(4.11)
Add the costs (4.9), (4.10) and (4.11) to get

$`C_{nk}`$ $``$ $`3s1.5+2\gamma f(s)+n+\mathrm{min}\{k,nk\}s+2gn/s+2ne^{2g^2/s}`$ (4.12b)
$`+6gn/s+\gamma f(\delta f(n))+\left[\mathrm{\hspace{0.17em}1.5}+\gamma \varphi (n)\right]4ne^{2g^2/s}`$
$``$ $`n+\mathrm{min}\{k,nk\}+\left[\mathrm{\hspace{0.17em}2}s+8gn/s+8ne^{2g^2/s}\right]`$
$`+\gamma \left[\mathrm{\hspace{0.17em}2}f(s)+f(\delta f(n))+4ne^{2g^2/s}\varphi (n)\right].`$
By (4.3)–(4.6), the bracketed term in (4.12b) is at most $`0.05\gamma f(n)`$ due to (4.8), and that in (4.12b) is at most $`0.95f(n)`$ from (4.7); thus (4.2) holds as required.        A

We now indicate briefly how to adapt the preceding proof to several variations on (4.1); choices similar to (4.13) and (4.17) are used in \[Meh00\] and \[FlR75a\], respectively.

###### Remarks 4.2

(a) Theorem 4.1 holds for the following modification of (4.1):

$$s:=\mathrm{min}\{\alpha f(n),n1\}\text{and}g:=(\beta s\mathrm{ln}\theta s)^{1/2}\text{with}f(n):=n^{2/3}\mathrm{ln}^{1/3}n,$$
(4.13)
provided that $`\beta 1/4`$, where $`\theta >0`$. Indeed, the analogue of (4.5) (cf. (4.1), (4.13))

$$gn/s=(\beta /\stackrel{~}{\alpha })^{1/2}f(n)(\mathrm{ln}\theta s/\mathrm{ln}n)^{1/2}(\beta /\alpha )^{1/2}f(n)(\mathrm{ln}\theta s/\mathrm{ln}n)^{1/2}$$
(4.14)
works like (4.5) for large $`n`$ (since $`lim_n\mathrm{}\frac{\mathrm{ln}\theta s}{\mathrm{ln}n}=2/3`$), whereas replacing (4.6) by

$$ne^{2g^2/s}=n(\theta s)^{2\beta }f(n)(\alpha \theta )^{2\beta }n^{(14\beta )/3}\mathrm{ln}^{(1+2\beta )/3}n,$$
(4.15)
we may replace $`\overline{n}^{1/32\beta }`$ by $`(\alpha \theta )^{2\beta }\overline{n}^{(14\beta )/3}`$ in (4.7)–(4.8).

(b) Theorem 4.1 holds for the following modification of (4.1):

$$s:=\mathrm{min}\{\alpha f(n),n1\}\text{and}g:=(\beta s\mathrm{ln}^{ϵ_l}n)^{1/2}\text{with}f(n):=n^{2/3}\mathrm{ln}^{ϵ_l/3}n,$$
(4.16)
provided either $`ϵ_l=1`$ and $`\beta 1/6`$, or $`ϵ_l>1`$. Indeed, since (4.16)$`=`$(4.1) for $`ϵ_l=1`$, suppose $`ϵ_l>1`$. Clearly, (4.3)–(4.5) hold with $`\varphi (t):=f(t)/t`$. For $`\stackrel{~}{\beta }1/6`$ and $`n`$ large enough, we have $`g^2/s=\beta \mathrm{ln}^{ϵ_l}n\stackrel{~}{\beta }\mathrm{ln}n`$; hence, replacing $`2\beta `$ by $`2\stackrel{~}{\beta }`$ and $`\mathrm{ln}^{1/3}`$ by $`\mathrm{ln}^{ϵ_l/3}`$ in (4.6)–(4.8), we may use the proof of Thm 4.1.

(c) Theorem 4.1 remains true if we use $`\beta 1/6`$,

$$s:=\mathrm{min}\{\alpha n^{2/3},n1\},g:=(\beta s\mathrm{ln}n)^{1/2}\text{and}f(n):=n^{2/3}\mathrm{ln}^{1/2}n.$$
(4.17)
Again (4.3)–(4.5) hold with $`\varphi (t):=f(t)/t`$, and $`\mathrm{ln}^{1/2}`$ replaces $`\mathrm{ln}^{1/3}`$ in (4.6)–(4.8).

(d) None of these choices gives $`f(n)`$ better than that in (4.1) for the bound (4.2).

### 4.2 Reischuk’s samples

For positive constants $`\alpha `$ and $`\beta `$, consider using

$$s:=\mathrm{min}\{\alpha n^{ϵ_s},n1\}\text{and}g:=\left(\beta sn^ϵ\right)^{1/2}\text{with}$$
(4.18a)
$$\eta :=\mathrm{max}\{\mathrm{\hspace{0.17em}1}+(ϵϵ_s)/2,ϵ_s\}<1\text{for some fixed}0<ϵ<ϵ_s.$$
(4.18b)
###### Theorem 4.3

Let $`C_{nk}`$ denote the expected number of comparisons made by Select for $`s`$ and $`g`$ chosen as in (4.2). There exists a positive constant $`\gamma _\eta `$ such that for all $`kn`$

$$C_{nk}n+\mathrm{min}\{k,nk\}+\gamma _\eta f_\eta (n)\text{with}f_\eta (n):=n^\eta .$$
(4.19)
* Proof. The function $`f_\eta (t):=t^\eta `$ grows to $`\mathrm{}`$ on $`(0,\mathrm{})`$, whereas $`\varphi _\eta (t):=f_\eta (t)/t=t^{\eta 1}`$ decreases to $`0`$, so $`f_\eta `$ and $`\varphi _\eta `$ may replace $`f`$ and $`\varphi `$ in the proof of Thm 4.1. Indeed, picking $`\overline{n}1`$ such that $`\alpha \overline{n}^{ϵ_s}\overline{n}1`$, for $`n\overline{n}`$ we may use $`s=\stackrel{~}{\alpha }n^{ϵ_s}\overline{\alpha }f_\eta (n)`$ with $`\alpha \stackrel{~}{\alpha }\overline{\alpha }:=1+1/\overline{n}^{ϵ_s}`$ to get analogues (4.3)–(4.4) and the following analogue of (4.5)

$$gn/s=(\beta /\stackrel{~}{\alpha })^{1/2}n^{1+(ϵϵ_s)/2}(\beta /\alpha )^{1/2}f_\eta (n).$$
(4.20)
Since $`g^2/s=\beta n^ϵ`$ by (4.2), and $`te^{2\beta t^ϵ}/t^\eta `$ decreases to $`0`$ for $`tt_\eta :=\left(\frac{1\eta }{2\beta ϵ}\right)^{1/ϵ}`$, we may replace (4.6) by

$$ne^{2g^2/s}=ne^{2\beta n^ϵ}\overline{n}^{1\eta }e^{2\beta \overline{n}^ϵ}f_\eta (n)n\overline{n}t_\eta .$$
(4.21)
Hence, with $`\overline{n}^{1\eta }e^{2\beta \overline{n}^ϵ}`$ replacing $`\overline{n}^{1/32\beta }\mathrm{ln}^{1/3}\overline{n}`$ in (4.7)–(4.8), the proof goes through.        A

###### Remarks 4.4

(a) For a fixed $`ϵ(0,1)`$, minimizing $`\eta `$ in (4.2) yields the optimal sample size parameter

$$ϵ_s:=(2+ϵ)/3,$$
(4.22)
with $`\eta =ϵ_s>2/3`$ and $`f_\eta (n)=n^{(2+ϵ)/3}`$; note that if $`s=\alpha n^{ϵ_s}`$ in (4.2), then $`g=(\alpha \beta )^{1/2}n^{ϵ_g}`$ with $`ϵ_g:=(1+2ϵ)/3`$. To compare the bounds (4.2) and (4.19) for this optimal choice, let $`\mathrm{\Phi }_ϵ(t):=(t^ϵ/\mathrm{ln}t)^{1/3}`$, so that $`\mathrm{\Phi }_ϵ(t)=f_\eta (t)/f(t)=\varphi _\eta (t)/\varphi (t)`$. Since $`lim_n\mathrm{}\mathrm{\Phi }_ϵ(n)=\mathrm{}`$, the choice (4.1) is asymptotically superior to (4.2). However, $`\mathrm{\Phi }_ϵ(n)`$ grows quite slowly, and $`\mathrm{\Phi }_ϵ(n)<1`$ even for fairly large $`n`$ when $`ϵ`$ is small (cf. Tab. 4.2).

On the other hand, for small $`ϵ`$ and $`\beta =1`$, the probability bound $`e^{2g^2/s}=e^{2n^ϵ}`$ of (4.2) is weak relative to $`e^{2g^2/s}=n^2`$ ensured by (4.1).

(b) Consider using $`s:=\mathrm{min}\{\alpha n^{ϵ_s},n1\}`$ and $`g:=\beta ^{1/2}n^{ϵ_g}`$ with $`ϵ_s,ϵ_g(0,1)`$ such that $`ϵ:=2ϵ_gϵ_s>0`$ and $`\eta :=\mathrm{max}\{1+ϵ_gϵ_s,ϵ_s\}<1`$. Theorem 4.3 covers this choice. Indeed, the equality $`1+ϵ_gϵ_s=1+(ϵϵ_s)/2`$ shows that (4.18b) and (4.20) remain valid, and we have the following analogue of (4.21)

$$ne^{2g^2/s}\overline{n}^{1\eta }e^{2(\beta /\overline{\alpha })\overline{n}^ϵ}f_\eta (n)n\overline{n}[(1\eta )\overline{\alpha }/(2\beta ϵ)]^{1/ϵ},$$
(4.23)
so compatible modifications of (4.7)–(4.8) suffice for the rest of the proof. Note that $`\eta (2+ϵ)/3`$ by (a); for the choice $`ϵ_s=\frac{1}{2}`$, $`ϵ_g=\frac{7}{16}`$ of \[Rei85\], $`ϵ=\frac{3}{8}`$ and $`\eta =\frac{15}{16}`$.

### 4.3 Handling small subfiles

Since the sampling efficiency decreases when $`X`$ shrinks, consider the following modification. For a fixed cut-off parameter $`n_{\mathrm{cut}}1`$, let sSelect$`(X,k)`$ be a “small-select” routine that finds the $`k`$th smallest element of $`X`$ in at most $`C_{\mathrm{cut}}<\mathrm{}`$ comparisons when $`|X|n_{\mathrm{cut}}`$ (even bubble sort will do). Then Select is modified to start with the following

Step 0 (Small file case). If $`n:=|X|n_{\mathrm{cut}}`$, return sSelect$`(X,k)`$.

Our preceding results remain valid for this modification. In fact it suffices if $`C_{\mathrm{cut}}`$ bounds the expected number of comparisons of sSelect$`(X,k)`$ for $`nn_{\mathrm{cut}}`$. For instance, (4.2) holds for $`nn_{\mathrm{cut}}`$ and $`\gamma C_{\mathrm{cut}}`$, and by induction as in Rem. 2.2(c) we have $`C_{nk}<\mathrm{}`$ for all $`n`$, which suffices for the proof of Thm 4.1.

Another advantage is that even small $`n_{\mathrm{cut}}`$ ($`1000`$ say) limits nicely the stack space for recursion. Specifically, the tail recursion of Step 7 is easily eliminated (set $`X:=\widehat{X}`$, $`k:=\widehat{k}`$ and go to Step 0), and the calls of Step 3 deal with subsets whose sizes quickly reach $`n_{\mathrm{cut}}`$. For example, for the choice of (4.1) with $`\alpha =1`$ and $`n_{\mathrm{cut}}=600`$, at most four recursive levels occur for $`n2^{31}2.1510^9`$.

## 5 Analysis of nonrecursive versions

Consider a nonrecursive version of Select in which Steps 3 and 7, instead of Select, employ a linear-time routine (e.g., Pick \[BFP+72\]) that finds the $`i`$th smallest of $`m`$ elements in at most $`\gamma _Pm`$ comparisons for some constant $`\gamma _P>2`$.

###### Theorem 5.1

Let $`c_{nk}`$ denote the number of comparisons made by the nonrecursive version of Select for a given choice of $`s`$ and $`g`$. Suppose $`s<n1`$.

(a)(a) For the choice of (4.1) with $`f(n):=n^{2/3}\mathrm{ln}^{1/3}n`$, we have

$$\mathrm{P}\left[c_{nk}n+\mathrm{min}\{k,nk\}+\widehat{\gamma }_Pf(n)\right]14n^{2\beta }\text{with}$$
(5.1a)
$$\widehat{\gamma }_P:=(4\gamma _P+2)(\beta /\alpha )^{1/2}+(2\gamma _P1)\left[\alpha +1/f(n)\right],$$
(5.1b)
also with $`f(n)`$ in (5.1b) replaced by $`f(3)>2`$ (since $`n3`$). Moreover, if $`\beta 1/6`$, then

$$\mathrm{E}c_{nk}n+\mathrm{min}\{k,nk\}+\left(\widehat{\gamma }_P+4\gamma _P+2\right)f(n).$$
(5.2)
(b)(a) For the choice of (4.13), if $`\theta sn`$, then (5.1a) holds with $`n^{2\beta }`$ replaced by $`(\alpha \theta )^{2\beta }n^{4\beta /3}\mathrm{ln}^{2\beta /3}n`$. Moreover, if $`\beta 1/4`$, then (5.2) holds with $`4\gamma _P+2`$ replaced by $`(4\gamma _P+2)(\alpha \theta )^{2\beta }`$.

(c)(a) For the choice of (4.2), (5.1) holds with $`f(n)`$ replaced by $`f_\eta (n):=n^\eta `$ and $`n^{2\beta }`$ by $`e^{2\beta n^ϵ}`$. Moreover, if $`n^{1\eta }e^{2\beta n^ϵ}1`$, then (5.2) holds with $`f`$ replaced by $`f_\eta `$.

* Proof. The cost $`c_{nk}`$ of Steps 3, 4 and 7 is at most $`2\gamma _Ps+c+\gamma _P\widehat{n}`$. By Cor. 3.6, the event $`𝒞:=\{c\overline{c},\widehat{n}<4gn/s\}`$ has probability $`\mathrm{P}[𝒞]14e^{2g^2/s}`$. If $`𝒞`$ occurs, then

$`c_{nk}`$ $``$ $`n+\mathrm{min}\{k,nk\}s+2gn/s+2\gamma _Ps+\gamma _P4gn/s`$ (5.3)
$``$ $`n+\mathrm{min}\{k,nk\}+\left(\mathrm{\hspace{0.17em}4}\gamma _P+2\right)gn/s+\left(\mathrm{\hspace{0.17em}2}\gamma _P1\right)s.`$
Similarly, since $`\mathrm{E}c_{nk}2\gamma _Ps+\mathrm{E}c+\gamma _P\mathrm{E}\widehat{n}`$, Lems. 3.43.5 yield

$$\mathrm{E}c_{nk}n+\mathrm{min}\{k,nk\}+\left(\mathrm{\hspace{0.17em}4}\gamma _P+2\right)gn/s+\left(\mathrm{\hspace{0.17em}2}\gamma _P1\right)s+\left(\mathrm{\hspace{0.17em}4}\gamma _P+2\right)ne^{2g^2/s}.$$
(5.4)
(a) Since $`e^{2g^2/s}=n^{2\beta }`$, $`s=\alpha f(n)\overline{\alpha }f(n)`$ from $`s<n1`$ and (4.3), and $`gn/s`$ is bounded by (4.5), (5.3) implies (5.1). Then (5.2) follows from (4.6) and (5.4).

(b) Proceed as for (a), invoking (4.14)–(4.15) instead of (4.5) and (4.6).

(c) Argue as for (a), using the proof of Thm 4.3, in particular (4.20)–(4.21).        A

###### Corollary 5.2

The nonrecursive version of Select requires $`n+\mathrm{min}\{k,nk\}+o(n)`$ comparisons with probability at least $`14n^{2\beta }`$ for the choice of (4.1), at least $`14(\alpha \theta )^{2\beta }n^{4\beta /3}`$ for the choice of (4.13), and at least $`14e^{2\beta n^ϵ}`$ for the choice of (4.2).

###### Remarks 5.3

(a) Suppose Steps 3 and 7 simply sort $`S`$ and $`\widehat{X}`$ by any algorithm that takes at most $`\gamma _S(s\mathrm{ln}s+\widehat{n}\mathrm{ln}\widehat{n})`$ comparisons for a constant $`\gamma _S`$. This cost is at most $`(s+\widehat{n})\gamma _S\mathrm{ln}n`$, because $`s,\widehat{n}<n`$, so we may replace $`2\gamma _P`$ by $`\gamma _S\mathrm{ln}n`$ and $`4\gamma _P`$ by $`4\gamma _S\mathrm{ln}n`$ in (5.3)–(5.4), and hence in (5.1)–(5.2). For the choice of (4.1), this yields

$$\mathrm{P}\left[c_{nk}n+\mathrm{min}\{k,nk\}+\widehat{\gamma }_Sf(n)\mathrm{ln}n\right]14n^{2\beta }\text{with}$$
(5.5a)
$$\widehat{\gamma }_S:=(4\gamma _S+2\mathrm{ln}^1n)(\beta /\alpha )^{1/2}+(\gamma _S\mathrm{ln}^1n)\left[\alpha +1/f(n)\right],$$
(5.5b)
$$\mathrm{E}c_{nk}n+\mathrm{min}\{k,nk\}+\left(\widehat{\gamma }_S+4\gamma _S+2\mathrm{ln}^1n\right)f(n)\mathrm{ln}n,$$
(5.6)
where $`\mathrm{ln}^1n`$ may be replaced by $`\mathrm{ln}^13`$, and (5.6) still needs $`\beta 1/6`$; for the choices (4.13) and (4.2), we may modify (5.3)–(5.6) as in Thm 5.1(b,c). Corollary 5.2 remains valid.

(b) The bound (5.2) holds if Steps 3 and 7 employ a routine (e.g., Find \[Hoa61\], \[AHU74, §3.7\]) for which the expected number of comparisons to find the $`i`$th smallest of $`m`$ elements is at most $`\gamma _Pm`$ (then $`\mathrm{E}c_{nk}2\gamma _Ps+\mathrm{E}c+\gamma _P\mathrm{E}\widehat{n}`$ is bounded as before).

(c) Suppose Step 6 returns to Step 1 if $`\widehat{n}4gn/s`$. By Cor. 3.6, such loops are finite wp $`1`$, and don’t occur with high probability, for $`n`$ large enough.

(d) Our results improve upon \[GeS96, Thm 2\], which only gives an estimate like (5.1a), but with $`4n^{2\beta }`$ replaced by $`O(n^{12\beta /3})`$, a much weaker bound. Further, the approach of \[GeS96, §3\] is restricted to distinct elements.

We now comment briefly on the possible use of sampling with replacement.

###### Remarks 5.4

(a) Suppose Step 2 of Select employs sampling with replacement. Since the tail bound (3.1) remais valid for the binomial distribution \[Chv79, Hoe63\], Lemma 3.3 is not affected. However, when Step 4 no longer skips comparisons with the elements of $`S`$, $`s`$ in (3.4) and (4.10) is replaced by $`0`$ (cf. the proof of Lem. 3.4), $`2s`$ in (4.12b) by $`3s`$ and $`2\overline{\alpha }`$ in (4.8) by $`3\overline{\alpha }`$. Similarly, adding $`s`$ to the right sides of (5.3)–(5.4) boils down to omitting $`1`$ in (5.1b) and $`\mathrm{ln}^1n`$ in (5.5b). Hence the preceding results remain valid.

(b) Of course, sampling with replacement needs additional storage for $`S`$. This is inconvenient for the recursive version, but tolerable for the nonrecursive ones because the sample sizes are relatively small (hence (3.4) with $`s`$ omitted is not too bad).

(c) Our results improve upon \[MoR95, Thm 3.5\], corresponding to (4.2) with $`ϵ=1/4`$ and $`\beta =1`$, where the probability bound $`1O(n^{1/4})`$ is weaker than our $`14e^{2n^{1/4}}`$, sampling is done with replacement and the elements are distinct.

(d) Our results subsume \[Meh00, Thm 2\], which gives an estimate like (5.2) for the choice (4.13) with $`\beta =1`$, using quickselect (cf. Rem. 5.3(b)) and sampling with replacement in the case of distinct elements.

## 6 Ternary and quintary partitioning

In this section we discuss ways of implementing Select when the input set is given as an array $`x[1:n]`$. We introduce a modification of the ternary partitioning scheme \[BeM93, BeS97\] that obviates subscript range checking, and give extensions to quintary partitioning.

The following notation is needed to describe the operations of Select in more detail.

Each stage works with a segment $`x[l:r]`$ of the input array $`x[1:n]`$, where $`1lrn`$ are such that $`x_i<x_l`$ for $`i=1:l1`$, $`x_r<x_i`$ for $`i=r+1:n`$, and the $`k`$th smallest element of $`x[1:n]`$ is the $`(kl+1)`$th smallest element of $`x[l:r]`$. The task of Select is extended: given $`x[l:r]`$ and $`lkr`$, Select$`(x,l,r,k,k_{},k_+)`$ permutes $`x[l:r]`$ and finds $`lk_{}kk_+r`$ such that $`x_i<x_k`$ for all $`li<k_{}`$, $`x_i=x_k`$ for all $`k_{}ik_+`$, $`x_i>x_k`$ for all $`k_+<ir`$. The initial call is Select$`(x,1,n,k,k_{},k_+)`$.

A vector swap denoted by $`x[a:b]x[b+1:c]`$ means that the first $`d:=\mathrm{min}(b+1a,cb)`$ elements of array $`x[a:c]`$ are exchanged with its last $`d`$ elements in arbitrary order if $`d>0`$; e.g., we may exchange $`x_{a+i}x_{ci}`$ for $`0i<d`$, or $`x_{a+i}x_{cd+1+i}`$ for $`0i<d`$.

### 6.1 Ternary partitions

For a given pivot $`v:=x_k`$ from the array $`x[l:r]`$, the following ternary scheme partitions the array into three blocks, with $`x_m<v`$ for $`lm<a`$, $`x_m=v`$ for $`amd`$, $`x_m>v`$ for $`d<mr`$. The basic idea is to work with the five inner parts of the array

| $`x<v`$ | $`x=v`$ | $`x<v`$ | ? | | $`x>v`$ | $`x=v`$ | $`x>v`$ |
| --- | --- | --- | --- | --- | --- | --- | --- |
| $`1^{2_{}^{3}{}_{}{}^{4}}`$ $`l`$ | $`\overline{l}`$ | $`p`$ | $`i`$ | $`j`$ | $`q`$ | $`\overline{r}`$ | $`r`$ |

(6.1)
until the middle part is empty or just contains an element equal to the pivot

| $`x=v`$ | $`x<v`$ | | $`x=v`$ | $`x>v`$ | | $`x=v`$ |
| --- | --- | --- | --- | --- | --- | --- |
| $`1^{2_{}^{3}{}_{}{}^{4}}`$ $`\overline{l}`$ | $`p`$ | $`j`$ |  | $`i`$ | $`q`$ | $`\overline{r}`$ |

(6.2)
(i.e., $`j=i1`$ or $`j=i2`$), then swap the ends into the middle for the final arrangement

<xv
=xv
>xv 1234 l¯adr¯.fragments
<xv
=xv
>xvfragments 1234 ¯𝑙𝑎𝑑¯𝑟\begin{tabular}[]{llrr}\hline\cr\vrule\lx@intercol\hfil$x<v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x>v$\hfil\lx@intercol\vrule\lx@intercol \\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$a$&$d$&$\bar{r}$\\
\end{tabular}\ . (6.3)
\[Initialize.\] Set $`v:=x_k`$ and exchange $`x_lx_k`$. Set $`i:=\overline{l}:=l`$, $`p:=l+1`$, $`j:=\overline{r}:=r`$, $`q:=r1`$. If $`v<x_r`$, set $`\overline{r}:=r1`$. If $`v>x_r`$, exchange $`x_lx_r`$ and set $`\overline{l}:=l+1`$.

\[Increase $`i`$ until $`x_iv`$.\] Increase $`i`$ by $`1`$; then if $`x_i<v`$, repeat this step.

\[Decrease $`j`$ until $`x_jv`$.\] Decrease $`j`$ by $`1`$; then if $`x_j>v`$, repeat this step.

\[Exchange.\] (Here $`x_jvx_i`$.) If $`i<j`$, exchange $`x_ix_j`$; then if $`x_i=v`$, exchange $`x_ix_p`$ and increase $`p`$ by $`1`$; if $`x_j=v`$, exchange $`x_jx_q`$ and decrease $`q`$ by $`1`$; return to A2. If $`i=j`$ (so that $`x_i=x_j=v`$), increase $`i`$ by $`1`$ and decrease $`j`$ by $`1`$.

\[Cleanup.\] Exchange $`x[\overline{l}:p1]x[p:j]`$ and $`x[i:q]x[q+1:\overline{r}]`$. Finally, set $`a:=\overline{l}+jp+1`$ and $`d:=\overline{r}q+i1`$.

Step A1 ensures that $`x_lvx_r`$, so steps A2 and A3 don’t need to test whether $`ij`$; thus their loops can run faster than those in the schemes of \[BeM93, BeS97\] and \[Knu97, Ex. 5.2.2–41\] (which do need such tests, since, e.g., there may be no element $`x_i>v`$).

### 6.2 Preparing for quintary partitions

At Step 1, $`rl+1`$ replaces $`n`$ in finding $`s`$ and $`g`$. At Step 2, it is convenient to place the sample in the initial part of $`x[l:r]`$ by exchanging $`x_ix_{i+\mathrm{rand}(ri)}`$ for $`lir_s:=l+s1`$, where $`\mathrm{rand}(ri)`$ denotes a random integer, uniformly distributed between $`0`$ and $`ri`$.

Step 3 uses $`k_u:=\mathrm{max}\{l1+is/mg,l\}`$ and $`k_v:=\mathrm{min}\{l1+is/m+g,r_s\}`$ with $`i:=kl+1`$ and $`m:=rl+1`$ for the recursive calls. If Select$`(x,l,r_s,k_u,k_u^{},k_u^+)`$ returns $`k_u^+k_v`$, we have $`v:=u:=x_{k_u}`$, so we only set $`k_v^{}:=k_v`$, $`k_v^+:=k_u^+`$ and reset $`k_u^+:=k_v1`$. Otherwise the second call Select$`(x,k_u^++1,r_s,k_v,k_v^{},k_v^+)`$ produces $`v:=x_{k_v}`$.

After $`u`$ and $`v`$ have been found, our array looks as follows

<xu
=xu
u<x<v
=xv
>xv
? 1234 lkuku+kvkv+rsr.fragments
<xu
=xu
u<x<v
=xv
>xv
?fragments 1234 lfragmentsk𝑢fragmentsk𝑢missing-subexpressionfragmentsk𝑣fragmentsk𝑣fragmentsr𝑠missing-subexpression𝑟\begin{tabular}[]{llrclrrlr}\hline\cr\vrule\lx@intercol\hfil$x<u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u<x<v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x>v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil?\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $l$&$k_{u}^{-}$&$k_{u}^{+}$&&$k_{v}^{-}$&$k_{v}^{+}$&$r_{s}$&&$r$\\
\end{tabular}\ . (6.4)
Setting $`\overline{l}:=k_u^{}`$, $`\overline{p}:=k_u^++1`$, $`\overline{r}:=rr_s+k_v^+`$, $`\overline{q}:=\overline{r}k_v^++k_v^{}1`$, we exchange $`x[k_v^++1:r_s]x[r_s+1:r]`$ and then $`x[k_v^{}:k_v^+]x[k_v^++1:\overline{r}]`$ to get the arrangement

<xu
=xu
u<x<v
?
=xv
>xv 1234 ll¯p¯q¯r¯r.fragments
<xu
=xu
u<x<v
?
=xv
>xvfragments 1234 l¯𝑙¯𝑝missing-subexpression¯𝑞¯𝑟𝑟\begin{tabular}[]{llllrrr}\hline\cr\vrule\lx@intercol\hfil$x<u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u<x<v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil?\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x>v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $l$&$\bar{l}$&$\bar{p}$&&$\bar{q}$&$\bar{r}$&$r$\\
\end{tabular}\ . (6.5)
The third part above is missing precisely when $`u=v`$; in this case (6.5) reduces to (6.1) with initial $`p:=\overline{p}`$, $`q:=\overline{q}`$, $`i:=p1`$ and $`j:=q+1`$. Hence the case of $`u=v`$ is handled via the ternary partitioning scheme of §6.1, with step A1 omitted.

### 6.3 Quintary partitions

For the case of $`k<(r+l)/2`$ and $`u<v`$, Step 4 may use the following quintary scheme to partition $`x[l:r]`$ into five blocks, with $`x_m<u`$ for $`lm<a`$, $`x_m=u`$ for $`am<b`$, $`u<x_m<v`$ for $`bmc`$, $`x_m=v`$ for $`c<md`$, $`x_m>v`$ for $`d<mr`$. The basic idea is to work with the six-part array stemming from (6.5)

| $`x=u`$ | $`u<x<v`$ | $`x<u`$ | ? | | $`x>v`$ | $`x=v`$ |
| --- | --- | --- | --- | --- | --- | --- |
| $`1^{2_{}^{3}{}_{}{}^{4}}`$ $`\overline{l}`$ | $`\overline{p}`$ | $`p`$ | $`i`$ | $`j`$ | $`q`$ | $`\overline{r}`$ |

(6.6)
until $`i`$ and $`j`$ cross

=xu
u<x<v
<xu
>xv
=xv 1234 l¯p¯pjiqr¯;fragments
=xu
u<x<v
<xu
>xv
=xvfragments 1234 ¯𝑙¯𝑝𝑝𝑗𝑖𝑞¯𝑟\begin{tabular}[]{lllrlrr}\hline\cr\vrule\lx@intercol\hfil$x=u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u<x<v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$xv$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$\bar{p}$&$p$&$j$&$i$&$q$&$\bar{r}$\\
\end{tabular}\ ; (6.7)
we may then swap the second part with the third one to bring it into the middle

| $`x=u`$ | $`x<u`$ | $`u<x<v`$ | | $`x>v`$ | | $`x=v`$ |
| --- | --- | --- | --- | --- | --- | --- |
| $`1^{2_{}^{3}{}_{}{}^{4}}`$ $`\overline{l}`$ | $`\overline{p}`$ | $`b`$ | $`c`$ | $`i`$ | $`q`$ | $`\overline{r}`$ |

(6.8)
and finally swap the extreme parts with their neighbors to get the desired arrangement

<xu
=xu
u<x<v
=xv
>xv 1234 l¯abcdr¯.fragments
<xu
=xu
u<x<v
=xv
>xvfragments 1234 ¯𝑙𝑎𝑏𝑐𝑑¯𝑟\begin{tabular}[]{lllrrr}\hline\cr\vrule\lx@intercol\hfil$x<u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u<x<v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x>v$\hfil\lx@intercol\vrule\lx@intercol \\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$a$&$b$&$c$&$d$&$\bar{r}$\\
\end{tabular}\ . (6.9)
\[Initialize.\] Set $`p:=k_v^{}`$, $`q:=\overline{q}`$, $`i:=p1`$ and $`j:=q+1`$.

\[Increase $`i`$ until $`x_iv`$.\] Increase $`i`$ by $`1`$. If $`x_iv`$, go to B3. If $`x_i<u`$, repeat this step. (At this point, $`ux_i<v`$.) If $`x_i=u`$, exchange $`x_ix_p`$ and $`x_px_{\overline{p}}`$ and increase $`\overline{p}`$ by $`1`$; otherwise exchange $`x_ix_p`$. Increase $`p`$ by $`1`$ and repeat this step.

\[Decrease $`j`$ until $`x_j<v`$.\] Decrease $`j`$ by $`1`$. If $`x_j>v`$, repeat this step. If $`x_j=v`$, exchange $`x_jx_q`$, decrease $`q`$ by $`1`$ and repeat this step.

\[Exchange.\] If $`ij`$, go to B5. Exchange $`x_ix_j`$. If $`x_i=u`$, exchange $`x_ix_p`$ and $`x_px_{\overline{p}}`$ and increase $`\overline{p}`$ and $`p`$ by $`1`$. Otherwise if $`x_i>u`$, exchange $`x_ix_p`$ and increase $`p`$ by $`1`$. If $`x_j=v`$, exchange $`x_jx_q`$ and decrease $`q`$ by $`1`$. Return to B2.

\[Cleanup.\] Set $`a:=\overline{l}+jp+1`$, $`b:=\overline{p}p+i`$, $`c:=j`$ and $`d:=\overline{r}q+i1`$. Swap $`x[\overline{p}:p1]x[p:j]`$, $`x[\overline{l}:\overline{p}1]x[\overline{p}:b1]`$, and finally $`x[i:q]x[q+1:\overline{r}]`$.

For the case of $`k(r+l)/2`$ and $`u<v`$, Step 4 may use the following quintary scheme, which is a symmetric version of the preceding one obtained by replacing (6.6)–(6.8) by

=xu
<xu
?
>xv
u<x<v
=xv 1234 l¯pijqq¯r¯,fragments
=xu
<xu
?
>xv
u<x<v
=xvfragments 1234 ¯𝑙𝑝𝑖𝑗𝑞¯𝑞¯𝑟\begin{tabular}[]{lllrrrr}\hline\cr\vrule\lx@intercol\hfil$x=u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$xv$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u<x<v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=v$\hfil\lx@intercol\vrule\lx@intercol \\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$p$&$i$&$j$&$q$&$\bar{q}$&$\bar{r}$\\
\end{tabular}\ , (6.10)
=xu
<xu
>xv
u<x<v
=xv 1234 l¯pjiqq¯r¯,fragments
=xu
<xu
>xv
u<x<v
=xvfragments 1234 ¯𝑙𝑝𝑗𝑖𝑞¯𝑞¯𝑟\begin{tabular}[]{llrlrrr}\hline\cr\vrule\lx@intercol\hfil$x=u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$xv$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u<x<v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$p$&$j$&$i$&$q$&$\bar{q}$&$\bar{r}$\\
\end{tabular}\ , (6.11)
<xu
=xu
u<x<v
>xv
=xv 1234 l¯abcq¯r¯.fragments
<xu
=xu
u<x<v
>xv
=xvfragments 1234 ¯𝑙𝑎𝑏𝑐¯𝑞¯𝑟\begin{tabular}[]{lllrrr}\hline\cr\vrule\lx@intercol\hfil$x<u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u<x<v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x>v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x=v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$a$&$b$&$c$&$\bar{q}$&$\bar{r}$\\
\end{tabular}\ . (6.12)
\[Initialize.\] Set $`p:=\overline{p}`$, $`q:=\overline{q}k_v^{}+k_u^++1`$, $`i:=p1`$ and $`j:=q+1`$, and swap $`x[\overline{p}:k_v^{}1]x[k_v^{}:\overline{q}]`$.

\[Increase $`i`$ until $`x_i>u`$.\] Increase $`i`$ by $`1`$. If $`x_i<u`$, repeat this step. If $`x_i=u`$, exchange $`x_ix_p`$, increase $`p`$ by $`1`$ and repeat this step.

\[Decrease $`j`$ until $`x_ju`$.\] Decrease $`j`$ by $`1`$. If $`x_ju`$, go to C4. If $`x_j>v`$, repeat this step. (At this point, $`u<x_jv`$.) If $`x_j=v`$, exchange $`x_jx_q`$ and $`x_qx_{\overline{q}}`$ and decrease $`\overline{q}`$ by $`1`$; otherwise exchange $`x_jx_q`$. Decrease $`q`$ by $`1`$ and repeat this step.

\[Exchange.\] If $`ij`$, go to C5. Exchange $`x_ix_j`$. If $`x_i=u`$, exchange $`x_ix_p`$ and increase $`p`$ by $`1`$. If $`x_j=v`$, exchange $`x_jx_q`$ and $`x_qx_{\overline{q}}`$ and decrease $`\overline{q}`$ and $`q`$ by $`1`$. Otherwise if $`x_j>v`$, exchange $`x_jx_q`$ and decrease $`q`$ by $`1`$. Return to C2.

\[Cleanup.\] Set $`a:=\overline{l}+jp+1`$, $`b:=i`$, $`c:=\overline{q}q+j`$ and $`d:=\overline{r}q+i1`$. Swap $`x[i:q]x[q+1:\overline{q}]`$, $`x[c+1:\overline{q}]x[\overline{q}+1:\overline{r}]`$, and finally $`x[\overline{l}:p1]x[p:j]`$.

To make (6.3) and (6.9) compatible, the ternary scheme may set $`b:=d+1`$, $`c:=a1`$. After partitioning $`l`$ and $`r`$ are updated by setting $`l:=b`$ if $`ak`$, then $`l:=d+1`$ if $`c<k`$; $`r:=c`$ if $`kd`$, then $`r:=a1`$ if $`k<b`$. If $`lr`$, Select may return $`k_{}:=k_+:=k`$ if $`l=r`$, $`k_{}:=r+1`$ and $`k_+:=l1`$ if $`l>r`$. Otherwise, instead of calling Select recursively, Step 6 may jump back to Step 1, or Step 0 if sSelect is used (cf. §4.3).

A simple version of sSelect is obtained if Steps 2 and 3 choose $`u:=v:=x_k`$ when $`rl+1n_{\mathrm{cut}}`$ (this choice of \[FlR75a\] works well in practice, but more sophisticated pivots could be tried); then the ternary partitioning code can be used by sSelect as well.

In fact steps A5, B5 and C5 may also share code: resetting $`\overline{q}:=q`$ and $`\overline{p}:=p`$ for A5, $`\overline{q}:=q`$ for B5, and $`\overline{p}:=p`$ for C5, we may swap $`x[\overline{p}:p1]x[p:j]`$ if $`p>\overline{p}`$, $`x[\overline{l}:\overline{p}1]x[\overline{p}:\overline{p}p+j]`$, $`x[i:q]x[q+1:\overline{q}]`$ if $`q<\overline{q}`$, $`x[i+\overline{q}q:\overline{q}]x[\overline{q}+1:\overline{r}]`$.

Even when outcomes of previous comparisons are utilized, our schemes still involve two extraneous comparisons (scheme A only one when $`i=j`$ at A4). Consider, therefore, the following alternative to scheme B, also based on the arrangements (6.6)–(6.9).

\[Initialize.\] Set $`p:=k_v^{}`$, $`q:=\overline{q}`$, $`i:=p`$, $`j:=q`$.

\[Increase $`i`$ until $`x_iv`$.\] If $`i>j`$ or $`x_iv`$, go to D3. If $`x_i=u`$, exchange $`x_ix_p`$ and $`x_px_{\overline{p}}`$, and increase $`p`$ and $`\overline{p}`$ by $`1`$; otherwise if $`x_i>u`$, exchange $`x_ix_p`$ and increase $`p`$ by $`1`$. Increase $`i`$ by $`1`$ and repeat this step.

\[Decrease $`j`$ until $`x_j<v`$.\] If $`i>j`$ or $`x_j<v`$, go to D4. If $`x_j=v`$, exchange $`x_jx_q`$ and decrease $`q`$ by $`1`$. Decrease $`j`$ by $`1`$ and repeat this step.

\[Exchange.\] If $`ij`$, go to D5. Exchange $`x_ix_j`$. If $`x_i=u`$, exchange $`x_ix_p`$ and $`x_px_{\overline{p}}`$ and increase $`\overline{p}`$ and $`p`$ by $`1`$. Otherwise if $`x_i>u`$, exchange $`x_ix_p`$ and increase $`p`$ by $`1`$. If $`x_j=v`$, exchange $`x_jx_q`$ and decrease $`q`$ by $`1`$. Increase $`i`$ by $`1`$, decrease $`j`$ by $`1`$, and return to D2.

\[Cleanup.\] Set $`a:=\overline{l}+jp+1`$, $`b:=\overline{p}p+i`$, $`c:=j`$ and $`d:=\overline{r}q+i1`$. Swap $`x[\overline{p}:p1]x[p:j]`$, $`x[\overline{l}:\overline{p}1]x[\overline{p}:b1]`$, and finally $`x[i:q]x[q+1:\overline{r}]`$.

Relative to scheme B (which makes $`rr_s+2`$ comparisons to $`v`$), scheme D saves two $`v`$-comparisons at the expense of $`rr_s+2`$ comparisons of $`i`$ vs. $`j`$. Since $`rr_s2`$ for usual choices of $`n_{\mathrm{cut}}`$, scheme B is faster than D unless the cost of key comparisons is extremely large. Scheme C compares in the same way with a symmetric variant of D. The situation with scheme A is similar, even for the small partitions produced in sSelect: although the schemes of \[BeM93, BeS97\] can save two $`v`$-comparisons, such savings are insignificant when relatively few small partitions occur (cf. §7.3).

### 6.4 Poor man’s partitions

We now consider a poor man’s version of Select, called pmSelect, which employs less refined but hopefully faster partitioning. This version works with $`x[l:r]`$ such that $`x_ix_l`$ for $`i=1:l1`$, $`x_rx_i`$ for $`i=r+1:n`$, and its task is standard: given $`x[l:r]`$ and $`lkr`$, pmSelect$`(x,l,r,k)`$ permutes $`x[l:r]`$ so that $`x_ix_k`$ for all $`li<k`$, and $`x_kx_i`$ for all $`k<ir`$; the initial call is pmSelect$`(x,1,n,k)`$. We start with binary partitions.

For a given pivot $`v:=x_k`$ from the array $`x[l:r]`$, the following binary scheme partitions the array into three blocks, with $`x_mv`$ for $`lm<a`$, $`x_m=v`$ for $`amd`$, $`vx_m`$ for $`d<mr`$; usually $`a=d`$ and the middle block is singleton.

\[Initialize.\] Set $`v:=x_k`$ and exchange $`x_lx_k`$. Set $`i:=\widehat{p}:=l`$ and $`j:=r`$. If $`v>x_r`$, exchange $`x_lx_r`$ and set $`\widehat{p}:=r`$. (Thus $`v=x_{\widehat{p}}`$ always.)

\[Increase $`i`$ until $`x_iv`$.\] Increase $`i`$ by $`1`$; then if $`x_i<v`$, repeat this step.

\[Decrease $`j`$ until $`x_jv`$.\] Decrease $`j`$ by $`1`$; then if $`x_j>v`$, repeat this step.

\[Exchange.\] (Here $`x_jvx_i`$.) If $`i<j`$, exchange $`x_ix_j`$ and return to E2. If $`i=j`$ (so that $`x_i=x_j=v`$), increase $`i`$ by $`1`$ and decrease $`j`$ by $`1`$.

\[Cleanup.\] If $`\widehat{p}r`$, exchange $`x_{\widehat{p}}x_j`$, set $`a:=j`$ and $`d:=i1`$; otherwise exchange $`x_ix_{\widehat{p}}`$, set $`a:=j+1`$ and $`d:=i`$.

The setup of §6.2 changes as follows. Step 3 calls pmSelect$`(x,l,r_s,k_u)`$ to find $`u:=x_{k_u}`$, and then pmSelect$`(x,k_u+1,r_s,k_v)`$ to get $`v:=x_{k_v}`$, assuming $`k_u<k_v`$; then (6.4) changes to

xu
u
uxv
v
xv
? 1234 lkukvrsr.fragments
xu
u
uxv
v
xv
?fragments 1234 lfragmentsk𝑢missing-subexpressionfragmentsk𝑣fragmentsr𝑠missing-subexpression𝑟\begin{tabular}[]{lcccrlr}\hline\cr\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil?\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $l$&$k_{u}$&&$k_{v}$&$r_{s}$&&$r$\\
\end{tabular}\ . (6.13)
Setting $`\overline{l}:=k_u`$, $`p:=k_v`$, $`\overline{r}:=rr_s+p`$, we exchange $`x[p+1:r_s]x[r_s+1:r]`$ and then $`x_px_{\overline{r}}`$ to get the arrangement

xu
u
uxv
?
v
xv 1234 ll¯pr¯r.fragments
xu
u
uxv
?
v
xvfragments 1234 l¯𝑙missing-subexpression𝑝missing-subexpression¯𝑟missing-subexpression𝑟\begin{tabular}[]{lcclrclr}\hline\cr\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil?\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $l$&$\bar{l}$&&$p$&&$\bar{r}$&&$r$\\
\end{tabular}\ . (6.14)
If $`u=v`$, setting $`i:=p1`$ and $`j:=\overline{r}`$, we may use scheme E with step E1 omitted and E5 replaced by A5 with $`q:=\overline{r}1`$; i.e., E5 now reads

\[Cleanup.\] Set $`a:=\overline{l}+jp+1`$ and $`d:=i`$. Swap $`x[\overline{l}:p1]x[p:j]`$ and $`x_dx_{\overline{r}}`$.

For the case of $`k<(r+l)/2`$ and $`u<v`$, we may use the following quintary scheme, obtained by replacing (6.6)–(6.8) with the arrangements

u
uxv
xu
?
xv
v 1234 l¯pijr¯,fragments
u
uxv
xu
?
xv
vfragments 1234 ¯𝑙missing-subexpression𝑝𝑖𝑗missing-subexpression¯𝑟\begin{tabular}[]{lcllrcl}\hline\cr\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil?\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol \\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&&$p$&$i$&$j$&&$\bar{r}$\\
\end{tabular}\ , (6.15)
u
uxv
xu
xv
v 1234 l¯pjir¯,fragments
u
uxv
xu
xv
vfragments 1234 ¯𝑙missing-subexpression𝑝𝑗𝑖¯𝑟\begin{tabular}[]{lclrlc}\hline\cr\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol \\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&&$p$&$j$&$i$&$\bar{r}$\\
\end{tabular}\ , (6.16)
u
xu
uxv
v
xv 1234 l¯ajir¯,fragments
u
xu
uxv
v
xvfragments 1234 ¯𝑙𝑎𝑗𝑖¯𝑟\begin{tabular}[]{lrrcr}\hline\cr\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$a$&$j$&$i$&$\bar{r}$\\
\end{tabular}\ , (6.17)
xu
u
uxv
v
xv 1234 l¯abcdr¯.fragments
xu
u
uxv
v
xvfragments 1234 ¯𝑙𝑎𝑏𝑐𝑑¯𝑟\begin{tabular}[]{lclrcr}\hline\cr\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$a$&$b$&$c$&$d$&$\bar{r}$\\
\end{tabular}\ . (6.18)
\[Initialize.\] Set $`i:=p1`$ and $`j:=\overline{r}`$.

\[Increase $`i`$ until $`x_iv`$.\] Increase $`i`$ by $`1`$. If $`x_iv`$, go to F3. If $`x_iu`$, repeat this step. (Here $`u<x_i<v`$.) Exchange $`x_ix_p`$, increase $`p`$ by $`1`$ and repeat this step.

\[Decrease $`j`$ until $`x_j<v`$.\] Decrease $`j`$ by $`1`$. If $`x_jv`$, repeat this step.

\[Exchange.\] If $`ij`$, go to F5. Exchange $`x_ix_j`$. If $`x_i>u`$, exchange $`x_ix_p`$ and increase $`p`$ by $`1`$. Return to F2.

\[Cleanup.\] Set $`a:=\overline{l}+ip`$, $`b:=a+1`$, $`c:=j`$ and $`d:=j+1`$. Swap $`x[\overline{l}+1:p1]x[p:j]`$, $`x_{\overline{l}}x_a`$ and finally $`x_dx_{\overline{r}}`$.

In fact scheme F may produce $`j<i1`$ on the first pass if $`x_mv`$ for all $`mk_v1`$.

For the case of $`k(r+l)/2`$ and $`u<v`$, we may use the following quintary scheme, which is a symmetric version of scheme F obtained by replacing (6.15)–(6.17) by

u
xu
?
xv
uxv
v1234 l¯ijqr¯,fragments
u
xu
?
xv
uxv
vfragments1234 ¯𝑙missing-subexpression𝑖𝑗𝑞missing-subexpression¯𝑟\begin{tabular}[]{cclrrcc}\hline\cr\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil?\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol \\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&&$i$&$j$&$q$&&$\bar{r}$\\
\end{tabular}\ , (6.19)
u
xu
xv
uxv
v1234 l¯jiqr¯,fragments
u
xu
xv
uxv
vfragments1234 ¯𝑙𝑗𝑖𝑞missing-subexpression¯𝑟\begin{tabular}[]{crlrccr}\hline\cr\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$j$&$i$&$q$&&$\bar{r}$\\
\end{tabular}\ , (6.20)
xu
u
uxv
xv
v 1234 l¯jidr¯.fragments
xu
u
uxv
xv
vfragments 1234 ¯𝑙𝑗𝑖𝑑¯𝑟\begin{tabular}[]{lcllcr}\hline\cr\vrule\lx@intercol\hfil$x\leq u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$u\leq x\leq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$x\geq v$\hfil\lx@intercol\vrule\lx@intercol &\vrule\lx@intercol\hfil$v$\hfil\lx@intercol\vrule\lx@intercol\\
\hline\cr\vphantom{$1^{{2^{3}}^{4}}$} $\bar{l}$&$j$&$i$&$d$&$\bar{r}$\\
\end{tabular}\ . (6.21)
\[Initialize.\] Set $`q:=rr_s+\overline{l}`$, $`i:=\overline{l}`$, $`j:=q+1`$, and swap $`x[\overline{l}+1:p1]x[p:\overline{r}1]`$.

\[Increase $`i`$ until $`x_i>u`$.\] Increase $`i`$ by $`1`$. If $`x_iu`$, repeat this step.

\[Decrease $`j`$ until $`x_ju`$.\] Decrease $`j`$ by $`1`$. If $`x_ju`$, go to G4. If $`x_jv`$, repeat this step. (Here $`u<x_j<v`$.) Exchange $`x_jx_q`$, decrease $`q`$ by $`1`$ and repeat this step.

\[Exchange.\] If $`ij`$, go to G5. Exchange $`x_ix_j`$. If $`x_j<v`$, exchange $`x_jx_q`$ and decrease $`q`$ by $`1`$. Return to G2.

\[Cleanup.\] Set $`a:=j`$, $`b:=a+1`$, $`d:=\overline{r}q+j`$ and $`c:=d1`$. Exchange $`x_{\overline{l}}x_a`$, $`x[i:q]x[q+1:\overline{r}1]`$, and finally $`x_dx_{\overline{r}}`$.

Also scheme G may produce $`j<i1`$ on the first pass if $`x_m<v`$ for all $`mq+1`$.

Schemes E, F and G are like A, B and C without their equality tests and associated updates. When equal elements are absent, the inequalities in (6.13)–(6.21) are strict, so E, F and G (although not equivalent to A, B and C) yield correct partitions for Step 4. When equal elements occur, the partitions of E, F and G needn’t meet the requirements of Step 4, but are still usable. Namely, $`rl`$ shrinks when $`a`$, $`b`$, $`c`$ and $`d`$ are used for updating $`l`$ and $`r`$, and sSelect may employ scheme E instead of A as in §6.3. In effect, pmSelect works like Select in the case of distinct elements, but may require more comparisons otherwise. In practice pmSelect tends to be slightly faster (cf. §7.3).

## 7 Experimental results

### 7.1 Implemented algorithms

An implementation of Select was programmed in Fortran 77 and run on a notebook PC (Pentium II 400 MHz, 256 MB RAM) under MS Windows 98. The input set $`X`$ was specified as a double precision array. For efficiency, the recursion was removed and small arrays with $`nn_{\mathrm{cut}}`$ were handled as if Steps 2 and 3 chose $`u:=v:=x_k`$; the resulting version of sSelect (cf. §§4.3 and 6.3) typically required less than $`3.5n`$ comparisons. The choice of (4.1) was employed, with the parameters $`\alpha =0.5`$, $`\beta =0.25`$ and $`n_{\mathrm{cut}}=600`$ as proposed in \[FlR75a\]; future work should test other sample sizes and parameters.

A similar implementation of pmSelect was programmed as described in §6.4.

For comparisons we developed a Fortran 77 implementation of the riSelect algorithm of \[Val00\]. Briefly, riSelect behaves like quickselect using the median of the first, middle and last elements, these elements being exchanged with randomly chosen ones only if the file doesn’t shrink sufficiently fast. To ensure $`O(n)`$ time in the worst case, riSelect may switch to the algorithm of \[BFP+72\], but this never happened in our experiments. Apparently riSelect represents the state-of-the-art in quickselect implementations (several other implementations fared worse in out tests).

### 7.2 Testing examples

We used minor modifications of the input sequences of \[Val00\], defined as follows:

A random permutation of the integers $`1`$ through $`n`$.

A random permutation of $`n/2`$ ones and $`n/2`$ zeroes.

The integers $`1`$ through $`n`$ in increasing order.

A sorted sequence rotated left once; i.e., $`(2,3,\mathrm{},n,1)`$.

The integers $`1`$ through $`n/2`$ in increasing order, followed by $`n/2`$ through $`1`$ in decreasing order.

Musser’s “median-of-3 killer” sequence with $`n=4j`$ and $`k=n/2`$:

$$\left(\begin{array}{ccccccccccccc}1& 2& 3& 4& \mathrm{}& k2& k1& k& k+1& \mathrm{}& 2k2& 2k1& 2k\\ 1& k+1& 3& k+3& \mathrm{}& 2k3& k1& 2& 4& \mathrm{}& 2k2& 2k1& 2k\end{array}\right).$$

Obtained by randomly permuting the elements of an m3killer sequence in positions $`4\mathrm{log}_2n`$ through $`n/21`$ and $`n/2+4\mathrm{log}_2n1`$ through $`n2`$.

For each input sequence, its (lower) median element was found.

These input sequences were designed to test the performance of selection algorithms under a range of conditions. In particular, the onezero sequences represent inputs containing many duplicates \[Sed77\]. The rotated and organpipe sequences are difficult for many implementations of quickselect. The m3killer and twofaced sequences are hard for implementations with median-of-3 pivots (their original versions \[Mus97\] were modified to become difficult when the middle element comes from position $`k`$ instead of $`k+1`$).

### 7.3 Computational results

We varied the input size $`n`$ from $`50,000`$ to $`16,000,000`$. For the random, onezero and twofaced sequences, for each input size, 20 instances were randomly generated; for the deterministic sequences, five runs were made to measure the solution time.

The performance of Select on randomly generated inputs is summarized in Table 7.1,

where the average, maximum and minimum solution times are in seconds, and the comparison counts are in multiples of $`n`$; e.g., column six gives $`C_{\mathrm{avg}}/n`$, where $`C_{\mathrm{avg}}`$ is the average number of comparisons made over all instances. Thus $`\gamma _{\mathrm{avg}}:=(C_{\mathrm{avg}}1.5n)/f(n)`$ estimates the constant $`\gamma `$ in the bound (4.2); moreover, we have $`C_{\mathrm{avg}}1.5L_{\mathrm{avg}}`$, where $`L_{\mathrm{avg}}`$ is the average sum of sizes of partitioned arrays. Further, $`P_{\mathrm{avg}}`$ is the average number of Select partitions, whereas $`N_{\mathrm{avg}}`$ is the average number of calls to sSelect and $`p_{\mathrm{avg}}`$ is the average number of sSelect partitions per call; both $`P_{\mathrm{avg}}`$ and $`N_{\mathrm{avg}}`$ grow slowly with $`\mathrm{ln}n`$. Finally, $`s_{\mathrm{avg}}`$ is the average sum of sample sizes; $`s_{\mathrm{avg}}/f(n)`$ drops from $`0.68`$ for $`n=50\mathrm{K}`$ to $`0.56`$ for $`n=16\mathrm{M}`$ on the random and twofaced inputs, and from $`0.57`$ to $`0.52`$ on the onezero inputs, whereas the initial $`s/f(n)\alpha =0.5`$. The average solution times grow linearly with $`n`$ (except for small inputs whose solution times couldn’t be measured accurately), and the differences between maximum and minimum times are fairly small (and also partly due to the operating system). Except for the smallest inputs, the maximum and minimum numbers of comparisons are quite close, and $`C_{\mathrm{avg}}`$ nicely approaches the theoretical lower bound of $`1.5n`$; this is reflected in the values of $`\gamma _{\mathrm{avg}}`$. Note that the results for the random and twofaced sequences are almost identical, whereas the onezero inputs only highlight the efficiency of our partitioning.

Table 7.2 exhibits similar features of Select on the deterministic inputs.

The results for the sorted and rotated sequences are almost the same, whereas the solution times on the organpipe and m3killer sequences are between those for the sorted and random sequences.

The performance of pmSelect on the same inputs is given in Tables 7.3 and 7.4.

Select is slower than pmSelect (but not too much: about 9% on random and twofaced, 44% on onezero, 16% on sorted and rotated, 13% on organpipe, 10% on m3killer). Except for timings and the onezero results, Tables 7.37.4 almost coincide with 7.17.2.

The performance of riSelect on the same inputs is described in Tables 7.5 and 7.6, where $`N_{\mathrm{rnd}}`$ denotes the average number of randomization steps.

Note that for riSelect, $`C_{\mathrm{avg}}L_{\mathrm{avg}}`$, i.e., the cost of median-of-3 finding is negligible. On the random sequences, the expected value of $`C_{\mathrm{avg}}`$ is of order $`2.75n`$ \[KMP97\], but Table 7.5 exhibits significant fluctuations in the numbers of comparisons made. The results for the onezero sequences confirm that quicksort-like partitioning may handle equal keys quite efficiently \[Sed77\]. The results for the twofaced, rotated and m3killer inputs are quite good, since some versions of quickselect may behave very poorly on these inputs \[Val00\] (note that we used the “sorted-median” partitioning variant as suggested in \[Val00\]). Finally, the median-of-3 strategy employed by riSelect really shines on the sorted inputs.

As always, limited testing doesn’t warrant firm conclusions, but a comparison of Select and riSelect is in order, especially for the random sequences, which are most frequently used in theory and practice for evaluating sorting and selection algorithms. On the random inputs, the ratio of the expected numbers of comparisons for riSelect and Select is asymptotically $`2.75/1.51.83`$; incidentally, the ratio of their computing times approaches $`3/1.621.85`$ (cf. Tabs. 7.1 and 7.5). Note that Select isn’t just asymptotically faster; in fact riSelect is about 50% slower even on middle-sized inputs. The same slow-down factor of about 50% is observed on the onezero sequences. The performance gains of Select over riSelect are much more pronounced on the remaining inputs, except for the sorted sequences on which Select may be slower by up to 88%. (However, the sorted input is quite special: increasing $`k`$ by $`1`$ (for the upper median) doubled the solution times of riSelect without influencing those of Select and pmSelect; e.g., for $`n=16\text{M}`$ the respective times were $`1.12`$, $`1.07`$ and $`0.90`$). Note that, relative to riSelect, the solution times and comparison counts of Select and pmSelect are much more stable across all the inputs. This feature may be important in applications.

Acknowledgment. I would like to thank Olgierd Hryniewicz, Roger Koenker, Ronald L. Rivest and John D. Valois for useful discussions.