TAOCP 5.3.3 Exercise 24

We need to solve Exercise 24 from TAOCP Volume 3, Section 5.

Section 5.3.3: Minimum-Comparison Selection

Exercise 24. [HM40] Prove that $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$ for $t \le \lceil n/2 \rceil$. Hint: Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located.

Verified: no
Solve time: 20m25s


We need to solve Exercise 24 from TAOCP Volume 3, Section 5.3.3. The exercise: Prove that $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$ for $t \le \lceil n/2 \rceil$. Hint: Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located.

First, we must understand the notation. $\overline{V}_t(n)$ is the average number of comparisons needed to find the $t$th largest element? Or perhaps the minimum average-case number of comparisons? The problem says "Prove that $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$". In the context of TAOCP, $V_t(n)$ is the minimum number of comparisons in the worst case. $\overline{V}_t(n)$ might denote the minimum average number of comparisons over all permutations? Or perhaps it's the expected number of comparisons for a randomized algorithm? The section context mentions "minimum number of comparisons needed to determine the tth largest of n elements" for $V_t(n)$. The exercise has a bar: $\overline{V}_t(n)$. That likely denotes the average-case minimum number of comparisons. But the problem statement doesn't define it; it's assumed from the book. In TAOCP, $\overline{V}_t(n)$ is the minimum average number of comparisons required to find the $t$th largest element, averaged over all $n!$ permutations, assuming all permutations equally likely. The exercise asks to prove an upper bound of $n + t + O(\sqrt{n \log n})$ for $t \le \lceil n/2 \rceil$.

The hint: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located."

We need to construct an algorithm that finds the $t$th largest element with average number of comparisons $n + t + O(\sqrt{n \log n})$. Or we need to prove that the average-case complexity is bounded by that. The hint suggests finding two order statistics that bracket $t$, then finding $t$ between them.

Let's think: There is a known result: The average number of comparisons to find the median is $1.5n + o(n)$ or something? Actually, the expected number of comparisons for selection algorithms like Quickselect is $2n + o(n)$? Wait, Quickselect average is $2n$? Let's recall: The average number of comparisons to find the $t$th order statistic using a simple randomized algorithm is $n + \min(t, n-t+1) + o(n)$? There is a known result by Hoare (Quickselect) that average comparisons = $2n + o(n)$ for median? Actually, the exact average for Quickselect to find the $t$th element is $2n + o(n)$? No, the average number of comparisons for Quickselect to find the median is about $1.5n$? Let's derive: The recurrence for expected comparisons in Quickselect: $C(n,t) = n-1 + \frac{1}{n} \sum_{i=1}^n ( \text{if } i=t \text{ then } 0 \text{ else if } i<t \text{ then } C(n-i, t-i) \text{ else } C(i-1, t) )$. The average is about $2n$? I recall that the expected number of comparisons for Quickselect to find the $k$th element is $2n + o(n)$? Actually, the average case of Quickselect is $2n + o(n)$ for finding the minimum or maximum? Wait, the average number of comparisons for Quickselect to find the median is $1.5n$? Let's check: For selection, there is a known result by Floyd and Rivest? There's an algorithm by Floyd and Rivest that finds the $t$th order statistic with $n + \min(t, n-t) + o(n)$ comparisons on average? Or maybe it's $n + t + o(n)$ for small $t$? The bound here is $n + t + O(\sqrt{n \log n})$, which for $t$ up to $n/2$ is $O(n)$? Actually $n + t$ is $O(n)$, and $O(\sqrt{n \log n})$ is sublinear. So this is a fairly tight bound for small $t$? If $t = n/2$, then $n + n/2 = 1.5n$, plus $O(\sqrt{n \log n})$. The median selection average is known to be $(3/2)n + o(n)$? Actually, the best known average case for median selection is $1.5n + o(n)$? Wait, there is a result by Dor and Zwick (1999) that median selection requires $(2 + \epsilon)n$? No, the lower bound is $2n - o(n)$? Let's check known results: The minimum average number of comparisons to find the median is known to be $(3/2)n + o(n)$? That doesn't sound right. Actually, the expected number of comparisons for the optimal algorithm to find the median is $(3/2)n + o(n)$? I think the best known upper bound for median selection is $1.5n + o(n)$? No, I recall that the minimum average number of comparisons to find the median is $1.5n + o(n)$? There's a paper by Kirkpatrick (1998?) or maybe by Dor and Zwick? Wait, the problem is from TAOCP, which is older. The exercise 24 is from the section on minimum-comparison selection. The bound $n + t + O(\sqrt{n \log n})$ suggests an algorithm that uses something like sampling to bracket the element.

The hint: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located."

So we need to find two order statistics: the $k_1$th and $k_2$th largest, where $k_1 = t - \sqrt{t \ln n}$ (floor) and $k_2 = t + \sqrt{t \ln n}$ (ceiling). Then the $t$th element lies between them. We can find the $t$th element by selecting from the elements between them? Actually, if we know the $k_1$th largest element $x$ and the $k_2$th largest element $y$, with $x \le y$ (since $k_1 < k_2$, $x$ is smaller or equal? Wait: The $k$th largest means: the largest is 1st largest. So $k_1$th largest is larger than $k_2$th largest if $k_1 < k_2$? Actually, 1st largest is the maximum, 2nd largest is second maximum, etc. So if $k_1 < k_2$, the $k_1$th largest is larger than the $k_2$th largest. So the $k_1$th largest is the $\lfloor t - \sqrt{t \ln n} \rfloor$th largest, which is larger than the $t$th largest, and the $k_2$th largest is smaller than the $t$th largest? Wait: $t$ is between $k_1$ and $k_2$, so $k_1 < t < k_2$. The $k_1$th largest is larger than the $t$th largest, and the $k_2$th largest is smaller than the $t$th largest. So we have an interval of elements: those that are less than or equal to the $k_1$th largest and greater than or equal to the $k_2$th largest? Actually, we know the values of the $k_1$th and $k_2$th largest. The $t$th largest must be between them in value. We can then restrict to the elements that lie between these two values. How many elements are there? The rank difference is $k_2 - k_1 \approx 2\sqrt{t \ln n}$. So we have about $2\sqrt{t \ln n}$ elements that could be the $t$th largest. We can then find the $t$th largest among these few elements by sorting them or using a selection algorithm. The total comparisons would be the comparisons to find the two bracketing order statistics plus the comparisons to find $t$ among the few elements.

The hint says "after which the $t$th is easily located." So we need to design an algorithm that finds these two order statistics with $n + t + O(\sqrt{n \log n})$ average comparisons. How to find the $k$th largest element on average with few comparisons? There's a known algorithm: Quickselect with a good pivot selection, or a sampling algorithm.

Let's recall the standard result: The average number of comparisons to find the $k$th smallest element using Quickselect is $2n + o(n)$? Actually, the average number of comparisons for Quickselect (Hoare's algorithm) to find the $k$th smallest is $2n + o(n)$? The recurrence: $C(n,k) = n-1 + \frac{1}{n} \sum_{i=1}^n [C(i-1,k) \text{ if } i > k \text{ else } C(n-i, k-i) \text{ if } i < k \text{ else } 0]$. For $k=1$ (minimum), $C(n,1) = n-1 + \frac{1}{n} \sum_{i=2}^n C(i-1,1)$. This yields $C(n,1) = n-1 + \frac{1}{n} \sum_{j=1}^{n-1} C(j,1)$. Solve: $C(n,1) = 2n - 2 - 2\sum_{j=1}^n 1/j?$ Actually, for minimum, we can just do linear scan: $n-1$ comparisons. Quickselect is overkill. The optimal algorithm for small $k$ is to find the $k$th largest by building a knockout tournament? The minimum worst-case for finding the $t$th largest is known to be $n + t + O(\log n)$? Wait, $V_t(n)$ is worst-case. The exercise is about average-case $\overline{V}_t(n)$.

The bound $n + t + O(\sqrt{n \log n})$ is interesting: For $t=1$, we need $n-1$ comparisons always, so $\overline{V}_1(n) = n-1$. The bound gives $n + 1 + O(\sqrt{n \log n})$, which is worse than the actual $n-1$. But the exercise says "for $t \le \lceil n/2 \rceil$". For $t=1$, $n-1 \le n+1+O(...)$ holds. So it's an upper bound, not necessarily tight.

The hint suggests finding two order statistics around $t$. This is reminiscent of the "median of medians" or sampling approach. But we need to prove an upper bound on the minimum average number of comparisons. So we can propose an algorithm and analyze its average comparisons.

We need to find the $\lfloor t - \sqrt{t \ln n} \rfloor$th largest and $\lceil t + \sqrt{t \ln n} \rceil$th largest. Let's denote $k_1 = t - \sqrt{t \ln n}$ (floor) and $k_2 = t + \sqrt{t \ln n}$ (ceiling). The difference $d = k_2 - k_1 \approx 2\sqrt{t \ln n}$.

How to find the $k$th largest element on average? One method: Use a sample of size $s$ to estimate the $k$th element. For selection, there's a known algorithm by Floyd and Rivest? Actually, the classic algorithm by Hoare (Quickselect) has average comparisons $2n + o(n)$. But we need $n + t + O(\sqrt{n \log n})$. That's much smaller than $2n$ for small $t$. For $t=1$, it's $n-1$ vs $n+1+...$ so it's fine. For $t$ small, say $t = \sqrt{n}$, then $n + \sqrt{n} + O(\sqrt{n \log n}) \approx n$, which is optimal? But we need to find the $t$th largest. If $t$ is small, we can find it by building a tournament? But the worst-case for finding the $t$th largest is $n + t + O(\log n)$? Actually, $V_t(n)$ worst-case is $n + t + O(\log n)$ for $t \le n/2$? I recall that $V_t(n) = n + t + O(\log n)$ for $t \le n/2$? There's a result by Kislitsyn or others. But this is average-case.

Maybe we can use the following algorithm: To find the $k$th largest, we can maintain a set of $k$ candidates and compare the rest against them. But that's worst-case $n + k \log k$? Not sure.

Wait, the problem is from TAOCP Vol 3, Section 5.3.3. The section discusses minimum-comparison selection. The exercise is HM40, meaning it's a hard problem requiring mathematical proof. The hint: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located."

So we need to construct an algorithm that finds these two order statistics, and then the $t$th is between them. The average number of comparisons should be bounded by $n + t + O(\sqrt{n \log n})$.

Let's think about how to find the $k$th largest element on average. There's a known algorithm: "Quickselect with median-of-three" or something? But we need a precise bound.

Maybe the algorithm is: Randomly choose a pivot, partition, and then recursively select. But the average comparisons for Quickselect is $2n + o(n)$. That's too high. The bound here is $n + t + ...$. For $t$ close to $n/2$, $n + t \approx 1.5n$, while Quickselect average is $2n$. So we need a better algorithm.

Perhaps the algorithm is: Use a sample of size $m$ to find an approximate rank, then narrow down. The idea: To find the $k$th largest, take a sample of size $s$, find its exact order statistics (by sorting), then use those to bound the rank of the desired element. Then we can eliminate many elements.

Let's derive the required algorithm. We want to find two order statistics $k_1$ and $k_2$ that bracket $t$. The gap is about $2\sqrt{t \ln n}$. If we can find these two elements with total comparisons $n + t + O(\sqrt{n \log n})$, then we can find the $t$th among the remaining elements (which are at most the gap size) by sorting them or using selection. The number of remaining elements is $O(\sqrt{t \ln n})$, so sorting them takes $O(\sqrt{t \ln n} \log \sqrt{t \ln n}) = O(\sqrt{t \ln n} (\log t + \log \ln n))$. But the bound is $O(\sqrt{n \log n})$. Since $t \le n/2$, $\sqrt{t \ln n} \le \sqrt{n \ln n}$, and sorting them would be $O(\sqrt{n \ln n} \log n)$, which is larger than $O(\sqrt{n \log n})$. So we need a better way to find the $t$th among the few elements. Perhaps we don't sort them; we just run a selection algorithm on them. But the number of comparisons to select among $m$ elements is $O(m)$. If $m = O(\sqrt{t \ln n})$, then $O(m)$ is $O(\sqrt{n \log n})$, which fits the bound. But we need to be careful: The total comparisons should be $n + t + O(\sqrt{n \log n})$. The $n + t$ part likely comes from finding the two bracketing elements.

How to find the $k$th largest with $n + k$ comparisons on average? There is a simple algorithm: To find the $k$th largest, we can maintain a set of $k$ largest elements seen so far. Initialize with the first $k$ elements (sorted). Then for each remaining element, compare it with the smallest in the set (the $k$th largest so far). If it's larger, we insert it into the set (which takes at most $\log k$ comparisons using binary search, or $k$ comparisons using linear scan). The total comparisons: $(k-1)$ to sort the first $k$? Actually, we can build a heap or use insertion. But the average number of insertions? If the input is random permutation, the probability that a new element is among the top $k$ is $k/(i)$ at step $i$. So the expected number of insertions is $\sum_{i=k+1}^n k/i \approx k \ln(n/k)$. Each insertion takes about $\log k$ comparisons (if we use a balanced BST or binary search in sorted array). So total comparisons $\approx k \ln(n/k) \log k + n$? That doesn't give $n + k$.

Wait, there's a known algorithm by Floyd and Rivest? No, there's an algorithm by Hoare (Quickselect) that averages $2n$. There's an algorithm by Schonhage, Paterson, and Pippenger? Not sure.

Let's re-read the hint: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." The phrase "with this many comparisons" refers to the bound $n + t + O(\sqrt{n \log n})$. So we need to show that we can find those two order statistics within that budget. Perhaps we can find them simultaneously? Or we find one, then the other?

Maybe the algorithm is: Use a sample of size $s = \sqrt{n \ln n}$ or something. Let's think about the standard technique for average-case selection: The "median of medians" is for worst-case. For average-case, we can use a random sample to get a good pivot. But the bound $n + t + O(\sqrt{n \log n})$ is very specific.

Let's search memory: There's a known result by Fussenegger and Gabow? Or maybe by Kirkpatrick? The exercise is from TAOCP, and the hint is from the author (Knuth). The section context mentions "tree selection" and "Kislitsyn's theorem". Exercise 24 is after Exercise 23 which asks for asymptotic value of $V_{\lceil n/2 \rceil}(n)$. That's worst-case median selection. Exercise 24 is about average-case.

Maybe $\overline{V}_t(n)$ is the average number of comparisons needed to find the $t$th largest, assuming all $n!$ permutations equally likely, and we use an optimal algorithm. The bound $n + t + O(\sqrt{n \log n})$ is a known upper bound.

Let's try to construct an algorithm that achieves this bound. The hint: find the $k_1$th and $k_2$th largest. If we find the $k_1$th largest, we can partition the elements into those larger than it and those smaller. But we need to find both.

Alternative approach: Use a two-stage algorithm. First, take a random sample of size $s$. Sort the sample. Then the desired order statistics $k_1$ and $k_2$ will fall between two order statistics of the sample with high probability. We can then find them by selecting among the elements that fall between those sample quantiles.

Let's formalize. Suppose we want to find the $k$th largest element (where $k$ is $k_1$ or $k_2$). We can take a sample of size $m$, sort it (cost $O(m \log m)$). Then we can find the rank of each sample element in the whole set by comparing all other elements with the sample? That would be $n \log m$ if we use binary search, or $n m$ if we scan. That's too many comparisons.

Maybe we use a different method: "Quickselect" but with a twist to reduce the average to $n + k$? Wait, there's a known result: The average number of comparisons to find the $k$th smallest is $n + \min(k, n-k+1) + o(n)$? Actually, there's an algorithm by Floyd and Rivest (1975) that finds the $k$th order statistic with $n + \min(k, n-k) + o(n)$ comparisons on average. But the bound here is $n + t + O(\sqrt{n \log n})$, which for $t \le n/2$ is $n + t + O(\sqrt{n \log n})$. That matches $n + \min(t, n-t+1)$ if $t \le n/2$? But $\min(t, n-t+1)$ is $t$ for $t \le (n+1)/2$. So it's $n + t + o(n)$. The $O(\sqrt{n \log n})$ is a specific error term.

Let's recall the algorithm by Floyd and Rivest: "Algorithm SELECT" (1975). They achieve $n + \min(k, n-k) + o(n)$ expected comparisons. The $o(n)$ term is $O(\sqrt{n \log n})$? Actually, their algorithm uses a sample of size $n^{2/3} \log^{1/3} n$? Not sure.

Wait, the exercise is from TAOCP, and the hint is given. So we can follow the hint: Show that we can find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements with that many comparisons. So maybe we can find one, then the other? Or find them simultaneously?

Let's think about finding the $k$th largest element. If we can find it with $n + k + O(\sqrt{n \log n})$ comparisons, then doing it for both $k_1$ and $k_2$ would give $2n + t + ...$ which is too much. So we need a more efficient way to find both.

Maybe we can find them by doing a single pass that identifies a narrow range containing both. The hint says "find both ... after which the $t$th is easily located." So we find two elements that bracket $t$, and then we only need to consider the elements between them. The number of elements between them is at most $k_2 - k_1 + 1 \approx 2\sqrt{t \ln n}$. If we can identify this set of elements, we can then find the $t$th among them with $O(\sqrt{t \ln n})$ comparisons. The total comparisons would be the cost to find the two bracketing elements plus the cost to select among the few elements.

How to find the two bracketing elements with $n + t + O(\sqrt{n \log n})$? Maybe we can find the $k_1$th largest and $k_2$th largest by first finding the $k_1$th largest, and then the $k_2$th largest among the remaining elements? But $k_2$ is larger than $k_1$, so the $k_2$th largest is smaller than the $k_1$th largest. If we find the $k_1$th largest, we know all elements larger than it (there are $k_1-1$ such elements). The $k_2$th largest is among the elements smaller than the $k_1$th largest. So we could recursively find the $(k_2 - k_1 + 1)$th largest among the smaller elements? That might add extra comparisons.

Alternatively, we can use a sampling approach to simultaneously estimate both. Let's try to design an algorithm that finds the $k$th largest with $n + k$ comparisons on average. There is a simple algorithm: "Repeatedly find the maximum and remove it"? That would take $n + (n-1) + ... + (n-k+1) = kn - k(k-1)/2$ comparisons, which is $kn$ for small $k$, not $n+k$.

Wait, there is an algorithm by Hoare, "Quickselect", which averages $2n$ comparisons. To get $n + k$, we need a different approach. Perhaps the algorithm is: "Build a heap of size $k$"? That takes $n + k \log k$? Not $n + k$.

Let's look at the literature: The average-case complexity of finding the $k$th order statistic is known to be $n + \min(k, n-k) + o(n)$ (Dor and Zwick, 1999? Actually, the exact constant for the $o(n)$ term might be known). The problem asks to prove $n + t + O(\sqrt{n \log n})$, which is a weaker bound (since $O(\sqrt{n \log n})$ is $o(n)$). So we can use a simpler algorithm than the optimal one.

Maybe we can use a variant of Quickselect where we choose the pivot as the median of a small sample? The average number of comparisons for Quickselect with median-of-3 is still $c n$ with $c > 1$. To get $n + k$, we need an algorithm that makes $n$ comparisons for partitioning plus $k$ extra.

Consider the following algorithm for finding the $k$th largest (where $k \le n/2$):

  1. Partition the elements into groups of size $m$ (some constant? or variable). Find the maximum in each group? Not sure.

Another idea: Use a "tournament" to find the maximum, then the second maximum, etc. But the worst-case for finding the $k$th largest using a tournament is $n + k \log n$? Actually, building a knockout tournament to find the maximum takes $n-1$ comparisons. To find the second maximum, we need $\lceil \log n \rceil - 1$ additional comparisons. To find the third, more. The total to find the $k$th largest using a tournament is $n + k \log n + ...$? Not $n + k$.

Wait, there's a result by Kislitsyn: $W_t(n) \le n - t + \sum_{n+1-t < j \le n} \lceil \lg j \rceil$. For small $t$, this is $n - t + t \lceil \lg n \rceil \approx n + t \log n$. That's worst-case. So tournament gives $n + t \log n$, not $n + t$.

The average-case bound $n + t$ is better. How to achieve $n + t$ on average? Perhaps we can use the fact that the input is a random permutation. We can do a linear scan to find the $k$th largest? No, linear scan can only find min or max.

Let's think about the algorithm that finds the $k$th largest with $n + k$ expected comparisons. I recall a simple algorithm: "Maintain the $k$ largest elements seen so far in a sorted list. For each new element, compare it with the smallest in the list (the current $k$th largest). If it is larger, insert it into the list (using binary search)." The number of comparisons per element: 1 to compare with the smallest, plus $\log k$ for insertion if it is larger. The probability that a random element is among the top $k$ is $k/(i)$ at step $i$ (where $i$ is the number of elements processed). So expected total comparisons: $(n - k) \cdot 1$ (for the comparison with the smallest) + $\sum_{i=k+1}^n \frac{k}{i} \log k$? That's $n + k \log k \cdot \ln(n/k)$, which is $n + O(k \log k \log n)$, not $n + k$.

To get $n + k$, we need the extra comparisons to be $O(k)$, not $O(k \log k \log n)$. How can we insert the new element into the list of $k$ largest in $O(1)$ amortized comparisons? If we use a linear scan from the smallest upward, we might need up to $k$ comparisons per insertion. But the expected number of elements that need to be scanned? If we insert into a sorted list of size $k$, the expected position of a random element among the top $k$ is uniform? Actually, if the new element is among the top $k$, its rank is uniformly distributed among $1..k$? Not exactly, but roughly. The expected number of comparisons to insert by linear scan from the smallest would be $k/2$, giving $O(k \log n)$ extra. Not $O(k)$.

Wait, maybe we can use a different strategy: "Quickselect with a good pivot" has average $2n$. The bound $n + k$ is less than $2n$ for $k < n$. For $k=1$, $n-1$ vs $n+1$, so it's not optimal for $k=1$ but it's an upper bound. For $k$ small, $n+k$ is close to $n$. Can we find the $k$th largest with $n + O(k)$ comparisons? Yes, there is an algorithm by Floyd and Rivest (1975) that does $n + \min(k, n-k) + o(n)$. Their algorithm uses a sample to estimate the $k$th element, then recursively selects. The $o(n)$ term is $O(n^{2/3} \log^{1/3} n)$ or something. But the exercise asks for $O(\sqrt{n \log n})$, which is larger than $n^{2/3}$ for large $n$? Actually, $\sqrt{n \log n}$ vs $n^{2/3}$: $n^{2/3}$ grows faster than $\sqrt{n \log n}$ for large $n$ (since $2/3 > 1/2$). So $O(\sqrt{n \log n})$ is a tighter bound on the error term. That suggests a different algorithm.

Maybe the algorithm is: Use a sample of size $s = \sqrt{n \ln n}$? Let's try to derive the bound.

We want to find the $t$th largest. Let $k_1 = \lfloor t - \sqrt{t \ln n} \rfloor$, $k_2 = \lceil t + \sqrt{t \ln n} \rceil$. We want to find both the $k_1$th and $k_2$th largest. The idea: Take a random sample of size $m$. Sort the sample. The sample will give us an approximate rank. We can then find the exact $k_1$th and $k_2$th by selecting among the elements that fall between two sample quantiles.

Let's analyze: Suppose we take a random sample $S$ of size $s$. Sort $S$ (cost $s \log s$). Then for each element not in $S$, we can compare it to the elements of $S$ to determine its rank among the sample? That would cost $n \log s$ if we binary search, or $n s$ if we linear scan. That's too much. Instead, we can use the sample to find a pivot, partition, and then recurse? That's Quickselect with sample pivot.

But the hint says "find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements". This suggests we might not need to find them exactly; we just need to find two elements that are guaranteed to bracket the $t$th element, with a small gap. Then we can find the $t$th within that gap.

Maybe we can use the following approach: We want to find the $t$th largest. We can run a "tournament" that eliminates elements that are definitely not the $t$th largest. The algorithm might be similar to the one used for median finding but with a different parameter.

Let's look at the section context. It mentions "tree selection" and "Kislitsyn's theorem". The exercises 21-23 are about worst-case bounds. Exercise 24 is about average-case. The hint: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located."

Maybe the algorithm is: Use a binary tree selection (like a heap) to find the $t$th largest? But the average-case of heap selection is $n + t \log n$? Not $n + t$.

Wait, I recall a known result: The average number of comparisons to find the $t$th largest is $n + t + O(\sqrt{n \log n})$. This might be a result by Mahmoud, Modarres, and Smythe? Or by Kirchhoff? Let's think about the algorithm that achieves this. There's a paper by "Floyd and Rivest" but that's from 1975. Knuth's book was written earlier? TAOCP Vol 3 was first published in 1973. The exercises might include later results. Exercise 24 is marked HM40, so it's a hard problem. The hint is from Knuth.

Let's try to reconstruct the proof from the hint. We need to show that $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$. We can do this by exhibiting an algorithm with that average comparison count. The algorithm should find the $k_1$th and $k_2$th largest with total comparisons $n + t + O(\sqrt{n \log n})$, and then find the $t$th among the remaining elements.

How to find the $k_1$th and $k_2$th largest? Perhaps we can find them simultaneously by a single pass that maintains a set of candidates. Or maybe we can find the $k_1$th largest, and then the $k_2$th largest is among the elements that are smaller than the $k_1$th largest. If we find the $k_1$th largest using an algorithm that takes $n + k_1$ comparisons, and then find the $k_2$th largest among the remaining $n - k_1$ elements? That would be $n + k_1 + (n - k_1) + (k_2 - k_1)$? Too large.

Maybe we can find the $k_2$th largest first, then the $k_1$th largest is among the elements larger than it? Still too many.

What if we find a pivot that splits the elements such that the $k_1$th and $k_2$th are in a small range? We can use a random sample to find two pivots that bracket the desired ranks.

Let's consider the following algorithm:

  1. Choose a random sample of size $s = \sqrt{n \ln n}$ (or something like that).
  2. Sort the sample completely.
  3. The sample divides the remaining elements into $s+1$ buckets. We can find the exact rank of each sample element by comparing all other elements to the sample? That would be $n s$ comparisons, too many.
  4. Instead, we can use the sample to find two elements that are close to the $k_1$th and $k_2$th ranks. Specifically, we can find the element in the sample that has rank approximately $k_1$ (or $k_2$). But we don't know the exact ranks of sample elements without comparing them to all other elements.

Wait, there's a known algorithm by Hoare (Quickselect) that uses a pivot. The average number of comparisons is $2n$. To reduce it to $n + t$, we can use the fact that for small $t$, we can find the $t$th largest by doing $t$ passes of finding the maximum? That would be $n + (n-1) + ...$ too large.

Maybe the algorithm is: "Find the $t$th largest by using a binary search on the value? Not applicable without a bounded universe.

Let's think differently. The bound $n + t + O(\sqrt{n \log n})$ is reminiscent of the expected number of comparisons for the "median of medians" algorithm? No, that's worst-case linear.

Perhaps we can use the following strategy: We want to find the $t$th largest. We can do a "partial sort" by building a heap of size $t$? Building a heap of size $t$ from the first $t$ elements takes $t$ comparisons? Actually, building a heap takes $O(t)$ comparisons. Then for each of the remaining $n-t$ elements, we compare with the root (the smallest in the heap). If larger, we replace the root and sift down (cost $\log t$). Total comparisons: $n - t$ (for the root comparisons) + expected number of sifts * $\log t$. The expected number of sifts is the number of elements that are among the top $t$. In a random permutation, each element has probability $t/n$ of being in the top $t$. So expected sifts = $(n-t) \cdot t/n \approx t$. So expected comparisons $\approx n - t + t \log t = n + t(\log t - 1)$. That's $n + O(t \log t)$, not $n + t$. For $t = \sqrt{n}$, this is $n + \sqrt{n} \log n$, which is $n + O(\sqrt{n} \log n)$, matching $O(\sqrt{n \log n})$? Actually, $t \log t = \sqrt{n} \cdot \frac{1}{2} \log n = \frac{1}{2} \sqrt{n \log n}$, which is $O(\sqrt{n \log n})$. But the bound is $n + t + O(\sqrt{n \log n})$, which for $t$ small (like constant) is $n + O(1)$, while heap gives $n + O(\log n)$. But the bound allows $O(\sqrt{n \log n})$ which is much larger than $\log n$ for large $n$. So heap is not optimal for small $t$, but the bound is not tight.

Wait, the bound is $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$. For $t$ for $t \le \lceil n/2 \rceil$. If $t$ is large, say $t = n/2$, then $n + t = 1.5n$, and $\sqrt{n \log n}$ is smaller than $n$. So the bound is $1.5n + o(n)$. Can we achieve $1.5n$ average comparisons for median? There is a known algorithm by "Floyd and Rivest" that does $1.5n + o(n)$? Actually, the best known average-case for median is $1.5n + o(n)$? I think the lower bound for median is $1.5n$? No, the lower bound for median in average case is $1.5n$? I recall a result by "Dor and Zwick" that the minimum average number of comparisons to find the median is $(3/2)n + o(n)$? Wait, I'm mixing up worst-case and average-case. Worst-case lower bound for median is $2n - o(n)$? Actually, the minimum worst-case comparisons to find the median is $3n/2 + o(n)$? No, the median can be found in $3n/2 + o(n)$ worst-case? There's a famous result by "Schonhage, Paterson, Pippenger" that median requires $3n/2 + o(n)$ worst-case? I'm not sure.

Let's focus on the hint. It says: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." This suggests we can find these two order statistics in $n + t + O(\sqrt{n \log n})$ comparisons total, and then we can find the $t$th by looking at the elements between them. The gap size is about $2\sqrt{t \ln n}$. If we have these two elements, we know their values. The $t$th element is the $(t - k_1)$th largest among the elements that are $\le k_1$th largest and $\ge k_2$th largest? Actually, if we have the $k_1$th largest element $x$ and the $k_2$th largest element $y$, with $k_1 < t < k_2$, then $x > y$ (since $k_1$th is larger). The $t$th largest is the $(t - k_1)$th largest among the elements that are less than $x$? Wait, the elements larger than $x$ are the top $k_1-1$. The $t$th largest is the $(t - (k_1-1))$th largest among the elements $\le x$? Let's be careful.

Let the elements be $a_1 > a_2 > ... > a_n$. The $k$th largest is $a_k$. We find $a_{k_1}$ and $a_{k_2}$ with $k_1 < t < k_2$. Then we know $a_{k_1}$ and $a_{k_2}$. The element $a_t$ is between them in value. We can find $a_t$ by selecting the $(t - k_1 + 1)$th largest among the elements that are $\le a_{k_1}$ and $\ge a_{k_2}$? Actually, the elements between $a_{k_1}$ and $a_{k_2}$ inclusive are $a_{k_1}, a_{k_1+1}, ..., a_{k_2}$. The number of such elements is $k_2 - k_1 + 1 \approx 2\sqrt{t \ln n} + 1$. We can find $a_t$ by sorting these elements or selecting the $(t - k_1 + 1)$th among them. The number of comparisons to do this is $O(\sqrt{t \ln n} \log \sqrt{t \ln n})$, which is $O(\sqrt{t \ln n} (\log t + \log \ln n))$. For $t$ up to $n/2$, this is $O(\sqrt{n \log n} \log n)$, which is larger than $O(\sqrt{n \log n})$. But the hint says "after which the $t$th is easily located." Maybe we don't need to sort them; we can just do a linear selection? The expected number of comparisons to find the $m$th order statistic among $M$ elements is $O(M)$ if we use Quickselect. But we need a deterministic bound? The average-case $\overline{V}t(n)$ is the minimum average over all algorithms. We can use a randomized algorithm. The expected comparisons to select the $m$th among $M$ is $2M + o(M)$. If $M = O(\sqrt{t \ln n})$, then $O(M) = O(\sqrt{n \log n})$, which fits the bound. But we need to ensure the total average is $n + t + O(\sqrt{n \log n})$. If we use Quickselect on the small set, it adds $O(\sqrt{n \log n})$ expected comparisons. So total expected comparisons = (comparisons to find $a{k_1}$ and $a_{k_2}$) + $O(\sqrt{n \log n})$. We need the first part to be $n + t + O(\sqrt{n \log n})$.

So the key is: How to find two order statistics $k_1$ and $k_2$ with total expected comparisons $n + t + O(\sqrt{n \log n})$?

Maybe we can find them sequentially: First find the $k_1$th largest using an algorithm that takes $n + k_1$ expected comparisons. Then, the $k_2$th largest is among the elements that are smaller than the $k_1$th largest. The number of such elements is $n - k_1$. We need to find the $(k_2 - k_1)$th largest among those? Actually, if we remove the $k_1$ largest elements (which we have identified? We only know the value of the $k_1$th largest, but we might not have identified all elements larger than it unless we partitioned around it). If we partition the set around the $k_1$th largest, we know which elements are larger than it (there are $k_1-1$ such elements) and which are smaller (there are $n - k_1$ elements). The $k_2$th largest is the $(k_2 - k_1)$th largest among the smaller elements? Wait: $k_2 > k_1$, so the $k_2$th largest is smaller than the $k_1$th largest. So it's the $(k_2 - k_1)$th largest among the elements smaller than the $k_1$th largest? Let's check: Elements smaller than $a_{k_1}$ are $a_{k_1+1}, ..., a_n$. The largest among them is $a_{k_1+1}$ (which is the $(k_1+1)$th largest overall). The $(k_2 - k_1)$th largest among them is $a_{k_1 + (k_2 - k_1)} = a_{k_2}$. Yes. So if we can find the $k_1$th largest and partition, then we need to find the $(k_2 - k_1)$th largest among the remaining $n - k_1$ elements. The expected comparisons for the first step: $n + k_1 + O(\sqrt{n \log n})$? The second step: the size is $n - k_1$, and the target rank is $k_2 - k_1 \approx 2\sqrt{t \ln n}$. If we use the same algorithm, the expected comparisons for the second step would be $(n - k_1) + (k_2 - k_1) + O(\sqrt{(n - k_1) \log(n - k_1)})$. Summing gives $n + k_1 + n - k_1 + k_2 - k_1 + ... = 2n + k_2 - k_1 + ...$, which is too large (about $2n$). So we can't do it sequentially with full $n$ each time.

We need a more efficient way to find both. Perhaps we can find them simultaneously by a single pass that maintains a "window" of candidates. For example, we can maintain the top $k_1$ elements and also the top $k_2$ elements? But $k_2 > k_1$, so the top $k_2$ includes the top $k_1$. Maintaining the top $k_2$ elements would cost $n + O(k_2 \log k_2)$? Not $n + t$.

Wait, maybe the algorithm is: Find the $k_1$th largest by an algorithm that takes $n + k_1$ comparisons, but in the process we also identify a set of candidates that includes the $k_2$th largest? Or we use a sample to get a good pivot that splits the array such that both $k_1$ and $k_2$ fall into a small subarray. Then we recursively select in that subarray.

Consider the following algorithm (similar to Floyd-Rivest):

  1. Take a random sample of size $s = \sqrt{n \ln n}$.
  2. Sort the sample.
  3. Use the sample to estimate the rank of the desired elements. Specifically, we want to find elements of rank $k_1$ and $k_2$. We can find the elements in the sample that have ranks approximately $k_1$ and $k_2$? But we don't know the exact ranks of sample elements without comparing them to all other elements.
  4. Instead, we can use the sample to select two pivots $p$ and $q$ such that with high probability, the true $k_1$th and $k_2$th elements lie between them. Then we partition the whole array using $p$ and $q$. This gives three groups: less than $p$, between $p$ and $q$, greater than $q$. The $k_1$th and $k_2$th elements will be in the middle group with high probability. The size of the middle group is small (about $2\sqrt{t \ln n}$). Then we can sort or select within that group. The cost: Sorting sample: $O(s \log s) = O(\sqrt{n \ln n} \log n)$. Comparing all elements to the sample? To partition using $p$ and $q$, we just need to compare each element to $p$ and $q$? Actually, we need to find $p$ and $q$ from the sample. If we take a sample of size $s$, we can find the $r_1$th and $r_2$th largest in the sample, where $r_1$ and $r_2$ are chosen to correspond to $k_1$ and $k_2$ in the whole set. Then we set $p$ and $q$ to be those sample elements. Then we partition the whole set by comparing each element to $p$ and $q$ (or first to $p$, then if greater to $q$?). The number of comparisons to partition is at most $2n$ (each element compared to both). That would give $2n + O(s \log s)$, which is $2n$, not $n + t$.

We need a partitioning scheme that uses $n + o(n)$ comparisons on average. How can we partition with $n$ comparisons? We can use a single pivot and do standard Quickselect partitioning, which takes $n-1$ comparisons. If we do Quickselect twice, we get $2n$. To get $n + t$, we need a method that finds the $t$th largest with only $n$ comparisons plus some extra.

Wait, there's a known algorithm by "Floyd and Rivest" that uses $n + \min(k, n-k) + o(n)$ comparisons. How does it work? It takes a random sample, finds the median of the sample, uses it as a pivot, partitions, and then based on the rank of the pivot, it either discards a large fraction or recurses. The expected number of comparisons is $n + \min(k, n-k) + o(n)$. The $n$ comes from partitioning the whole array once? Actually, they partition the array multiple times? No, the algorithm SELECT by Floyd and Rivest (1975) works as follows: If $n$ is small, sort. Otherwise, choose a sample of size $n^{2/3} \log^{1/3} n$, find its median, use it as pivot, partition, and then recurse on the appropriate side. The expected number of comparisons is $n + \min(k, n-k) + o(n)$. The $n$ term is the partitioning step? But partitioning takes $n-1$ comparisons. If we do it only once, we get $n$ plus recursion cost. The recursion cost is $o(n)$ if the sample is chosen appropriately. So total expected comparisons = $n + \min(k, n-k) + o(n)$. That matches the bound $n + t + o(n)$ for $t \le n/2$. The $o(n)$ term in their paper is $O(n^{2/3} \log^{1/3} n)$. But the exercise asks for $O(\sqrt{n \log n})$. Is $O(\sqrt{n \log n})$ a weaker bound than $O(n^{2/3} \log^{1/3} n)$? For large $n$, $\sqrt{n \log n}$ grows slower than $n^{2/3} \log^{1/3} n$? Let's compare exponents: $1/2$ vs $2/3$. $2/3 > 1/2$, so $\sqrt{n \log n}$ is smaller than $n^{2/3} \log^{1/3} n$ for large $n$. So the bound $O(\sqrt{n \log n})$ is tighter. But Floyd-Rivest algorithm achieves $O(n^{2/3} \log^{1/3} n)$ which is larger, so it does not meet the bound. However, there might be a simpler algorithm that achieves $O(\sqrt{n \log n})$ by a different choice of sample size.

Maybe we can use a sample of size $\sqrt{n \ln n}$ and then do something else? Let's analyze the Floyd-Rivest algorithm with sample size $s$. The expected number of comparisons is $n + \min(k, n-k) + O(s \log s + n \log n / s)$? Actually, the error term comes from the probability that the pivot is far from the true rank. If we choose a sample of size $s$, the rank of the sample median is approximately $n/2$ with error $O(n / \sqrt{s})$ by Chernoff bounds? Actually, the sample median's rank in the whole array has standard deviation $O(\sqrt{n^2/s}) = O(n/\sqrt{s})$. So the pivot will be within $O(n/\sqrt{s})$ of the true median. If we want to find the $k$th element, we might need to recurse on a subarray of size $O(n/\sqrt{s})$. The recursion depth might be 2 if we choose $s$ large enough. The total expected comparisons = $n$ (first partition) + size of subarray (second partition) + sorting sample? Actually, if we do two levels of partitioning, total comparisons = $n + n/\sqrt{s} + s \log s$. To minimize, set $s \approx n^{2/3}$ giving $n + n^{2/3} \log n$. To get $O(\sqrt{n \log n})$, we need $n/\sqrt{s} = O(\sqrt{n \log n})$, i.e., $\sqrt{s} = n / \sqrt{n \log n} = \sqrt{n/\log n}$, so $s = n/\log n$. Then $s \log s = n$. That gives $n + n = 2n$, not $n + t$. So that approach doesn't give $n + t$.

Wait, the bound is $n + t + O(\sqrt{n \log n})$. For $t$ small, $n + t \approx n$. For $t$ large, $t \approx n/2$, so $n + t \approx 1.5n$. The Floyd-Rivest algorithm gives $n + t + O(n^{2/3} \log^{1/3} n)$. For $t = n/2$, that's $1.5n + O(n^{2/3} \log^{1/3} n)$. Since $O(\sqrt{n \log n})$ is smaller than $O(n^{2/3} \log^{1/3} n)$, the Floyd-Rivest algorithm does not achieve this tighter bound. But the exercise is to prove that such a bound exists, so there must be an algorithm that achieves it.

Maybe the algorithm is not Floyd-Rivest but a different one. Let's think about the hint again: "find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements". This suggests we find two elements that are at a distance $\sqrt{t \ln n}$ from $t$. The gap is $2\sqrt{t \ln n}$. If we can find these two elements, the $t$th is between them. How to find them? Perhaps we can use a single pivot to find one of them, and then the other is easily found because the gap is small? Or maybe we use a tournament-like structure?

Another thought: The average-case complexity of selection can be analyzed using the "entropy" of the permutation? There's a known result by "Kirkpatrick" or "Kislitsyn"? Exercise 23 asks for asymptotic value of $V_{\lceil n/2 \rceil}(n)$, which is worst-case median selection. Exercise 24 is average-case.

Maybe the algorithm is: Use a binary search tree? Insert elements into a balanced BST and keep track of sizes? That would take $n \log n$ on average.

Let's search my memory for "average number of comparisons to find the t-th largest n + t + O(sqrt(n log n))". I recall a paper by "Mahmoud, Modarres, and Smythe" (1995) "Analysis of quickselect: an algorithm for order statistics" which gives the expected number of comparisons as $2n + o(n)$ for Quickselect. But that's $2n$. There's a paper by "Kirchhoff" (2018?) "On the average number of comparisons for selection"? Not sure.

Wait, maybe the bound $n + t + O(\sqrt{n \log n})$ is achieved by a simple algorithm: "Find the $t$th largest by repeatedly finding the maximum and removing it"? That takes $n + (n-1) + ... + (n-t+1) = tn - t(t-1)/2$. For $t=1$, it's $n-1$. For $t$ small, it's $tn$, which is larger than $n+t$.

What about the algorithm: "Use a heap of size $t$"? That gives $n + t \log t$ on average. For $t$ up to $\sqrt{n}$, $t \log t \le \sqrt{n} \log n$, which is $O(\sqrt{n \log n})$. For $t$ larger than $\sqrt{n}$, $t \log t$ might be larger than $O(\sqrt{n \log n})$. But the bound is $n + t + O(\sqrt{n \log n})$, so if $t$ is large, the $t$ term dominates, and the $O(\sqrt{n \log n})$ is negligible. The heap algorithm gives $n + t \log t$, which for $t = n/2$ is $n + (n/2) \log n$, which is much larger than $n + n/2 + O(\sqrt{n \log n})$. So heap is not good for large $t$.

So we need an algorithm that works well for all $t \le n/2$. The Floyd-Rivest algorithm gives $n + t + O(n^{2/3} \log^{1/3} n)$. But the exercise asks for $O(\sqrt{n \log n})$. Is there an algorithm with $O(\sqrt{n \log n})$? Maybe we can use a sample of size $s = \sqrt{n \ln n}$ and do a single partition? Let's see: If we choose a pivot from a sample of size $s$, the pivot's rank is roughly $n/2$ with standard deviation $n/\sqrt{s}$. To get a pivot within $\sqrt{t \ln n}$ of the target rank $t$, we need $n/\sqrt{s} \le \sqrt{t \ln n}$. So $\sqrt{s} \ge n / \sqrt{t \ln n}$. Since $t \le n/2$, the worst case is $t$ small. For $t$ small, $n/\sqrt{t \ln n}$ can be larger than $\sqrt{n}$. So $s$ might need to be larger than $n$, which is impossible. So a single pivot won't work for small $t$.

Wait, the hint says to find the $k_1$th and $k_2$th elements. If $t$ is small, $k_1 = t - \sqrt{t \ln n}$ might be negative? The floor is at least 1? The problem says $t \le \lceil n/2 \rceil$, so $t$ could be 1. For $t=1$, $k_1 = \lfloor 1 - \sqrt{\ln n} \rfloor \le 0$, which doesn't make sense. So maybe the formula is for $t$ sufficiently large? The hint says $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th. For small $t$, we can just use a different method (like linear scan for min/max). The bound is asymptotic, so we can assume $t$ is large enough that $\sqrt{t \ln n} \le t$, i.e., $t \ge \ln n$. For $t < \ln n$, the bound $n + t + O(\sqrt{n \log n})$ is just $n + O(\sqrt{n \log n})$, and we can achieve that by sorting? Actually, if $t$ is small, we can find the $t$th largest in $n + t \log n$ using a heap, which is $n + O(\ln n \log \ln n) \ll O(\sqrt{n \log n})$. So the bound holds trivially for small $t$. So we can assume $t$ is large enough that $\sqrt{t \ln n} \le t/2$ maybe.

Let's think about the algorithm that finds both $k_1$ and $k_2$ with $n + t$ comparisons. There's a known algorithm by "Schönheim" or "Kislitsyn"? Wait, the section is about minimum-comparison selection, which is usually worst-case. The average-case might be tackled by a different method.

Maybe we can use the following strategy: We want to find the $t$th largest. We can run a "tournament" that finds the maximum, second maximum, etc., but we stop early? The worst-case for finding the $t$th largest using a tournament is $n + t \log n$? Actually, to find the $t$th largest, we can build a heap of size $t$? That's $n + t \log t$? No, building a heap of size $t$ from $n$ elements: insert first $t$ elements into a heap (cost $t$). Then for each remaining element, compare to root, if larger, replace and sift down (cost $\log t$). The expected number of replacements is about $t \ln(n/t)$. So total expected comparisons = $t + (n-t) + t \ln(n/t) \log t \approx n + t \ln n \log t$. That's not $n + t$.

What if we use a "binary insertion" for the first $t$ elements? That's $t \log t$ to sort them, then $n \log t$ to insert? That's $n \log t$.

Maybe the algorithm is: "Find the $k_1$th and $k_2$th by doing a single pass that maintains a set of size $k_2$ of the largest elements?" That would cost $n + k_2 \log k_2$? Not $n + t$.

Let's re-read the hint carefully: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." The phrase "with this many comparisons" refers to the bound $n + t + O(\sqrt{n \log n})$. So the algorithm's total comparisons are at most $n + t + O(\sqrt{n \log n})$. So we need to find both order statistics within that budget. That means the algorithm to find both must use at most $n + t + O(\sqrt{n \log n})$ comparisons. Then the "easily located" step must use $O(\sqrt{n \log n})$ additional comparisons, which is absorbed in the $O$ term.

How can we find two order statistics with total $n + t$ comparisons? If we find the $k_1$th largest, we partition the array. The $k_2$th largest is among the elements smaller than the $k_1$th largest. If we could find the $k_1$th largest with $n$ comparisons, and then find the $k_2$th largest among the smaller elements with $t$ comparisons? But finding the $k_1$th largest in $n$ comparisons is impossible unless $k_1=1$ (min/max). So we must be doing something else.

Maybe the algorithm is not partitioning the whole array. Perhaps we use a sample to narrow down the candidates, and then we only compare the remaining candidates to the whole array? For example, we take a sample of size $s$, find the $k_1$th and $k_2$th largest in the sample? But we need the $k_1$th and $k_2$th largest in the whole set.

Wait, there's a known algorithm by "Hoare" (Quickselect) but with a twist: we can find the $t$th largest by doing a binary search on the value? Not without a bounded universe.

Another idea: The average number of comparisons to find the $t$th largest is equal to the expected number of comparisons in a decision tree that is optimal for the uniform distribution. There's a known result by "Kirkpatrick" (1998) "An optimal algorithm for selecting the median"? That's worst-case.

Maybe the algorithm is: "Use a tournament to find the maximum, then remove it, find the next maximum, etc., but do it in a way that reuses comparisons?" That's the standard tournament for finding the $t$th largest. The worst-case comparisons is $n + t \lceil \log n \rceil$? Actually, the standard knockout tournament for finding the maximum takes $n-1$. To find the second maximum, we only need to compare the $\lceil \log n \rceil$ opponents of the maximum, which takes $\lceil \log n \rceil - 1$ more. To find the third, we need to compare the opponents of the second maximum? The total to find the $t$th largest is $n + t \lceil \log n \rceil$? That's worst-case, not average-case.

For average-case, we can use a different structure: a "binary search tree" built by inserting elements in random order. The expected depth of the $t$th largest? Not sure.

Let's look at the section context again. It mentions "tree selection" and "Kislitsyn's theorem". Exercise 24 is HM40. The hint is given. Perhaps the intended algorithm is the "Floyd-Rivest" algorithm but with a specific choice of parameters that yields $O(\sqrt{n \log n})$. Let's check if $O(\sqrt{n \log n})$ is achievable with a two-level sample.

Suppose we take a random sample of size $s = \sqrt{n \ln n}$. Sort the sample. Then we find the element in the sample that has rank $r$ such that its expected rank in the whole set is $t$? We don't know its rank in the whole set without comparing it to all elements. But we can use the sample to partition the whole set. If we take the sample element that is the $r$th largest in the sample, and use it as a pivot to partition the whole set, the expected number of elements larger than the pivot is about $n \cdot (r/s)$. The variance is $O(n^2/s)$. To get the pivot's rank to be within $O(\sqrt{t \ln n})$ of $t$, we need $n/\sqrt{s} = O(\sqrt{t \ln n})$. So $s = \Omega(n^2/(t \ln n))$. For $t = n/2$, this gives $s = \Omega(n / \ln n)$. Then sorting sample costs $s \log s = O(n)$. Partitioning costs $n$. Total $O(n)$. But we need $n + t$. If we do this twice, we get $2n$. To get $n + t$, we need to do only one partition for the whole array? Or maybe we do a partition that splits into three parts using two pivots, and we only recurse on the middle part.

Consider the following algorithm for finding the $t$th largest:

  1. Choose a random sample of size $s = \sqrt{n \ln n}$.
  2. Sort the sample.
  3. Let $a$ be the element in the sample with rank $\alpha s$ where $\alpha = t/n$. Let $b$ be the element with rank $\beta s$ where $\beta = t/n$? Actually, we want two pivots that bracket $t$. We can take the $r_1$th and $r_2$th order statistics of the sample, where $r_1$ and $r_2$ are chosen such that the true ranks of these pivots are around $k_1$ and $k_2$. But we don't know the true ranks without comparing to all elements.
  4. Instead, we can take the whole array and compare each element to the two pivots? That would be $2n$ comparisons.

Maybe we can use a different approach: "Find the $k_1$th largest by an algorithm that takes $n + k_1$ comparisons, and during that process we also identify the $k_2$th largest?" There is an algorithm by "Schönheim" or "Kislitsyn" for worst-case that builds a tree and selects. The average-case of that algorithm might be $n + t + O(\sqrt{n \log n})$.

Let's search my memory for "n + t + O(sqrt(n log n))" in the context of selection. I recall a result by "M. D. Atkinson" or "S. J. A. G. K. ..."? There's a paper "The average number of comparisons in finding the median" by Knuth? In TAOCP, there is an exercise about average-case selection. The hint mentions $\sqrt{t \ln n}$. This looks like a Chernoff bound. The algorithm might be: Use a random sample to estimate the rank, then use a "tournament" on the remaining elements?

Wait, maybe the algorithm is: We want to find the $t$th largest. We can first find the maximum? No.

Let's think about the phrase "after which the $t$th is easily located." If we have the $k_1$th and $k_2$th elements, we know their values. The $t$th element is the $(t - k_1 + 1)$th largest among the elements that are $\le$ the $k_1$th element and $\ge$ the $k_2$th element. The number of such elements is $k_2 - k_1 + 1 = O(\sqrt{t \ln n})$. We can find the $t$th by simply comparing all these elements with each other? That would take $O((\sqrt{t \ln n})^2) = O(t \ln n)$ comparisons, which is too much. But we can do a selection among them in $O(\sqrt{t \ln n})$ comparisons on average? Actually, to find the $m$th order statistic among $M$ elements, the minimum average comparisons is $M + \min(m, M-m) + o(M)$? If $M = O(\sqrt{t \ln n})$, then $O(M) = O(\sqrt{n \log n})$. So we can just use the same algorithm recursively on this small set, or even sort them with $O(M \log M)$ which is $O(\sqrt{n \log n} \log n)$, which is $O(\sqrt{n} \log^{3/2} n)$, not $O(\sqrt{n \log n})$? Actually, $\sqrt{n \log n} \log n = \sqrt{n} \log^{3/2} n$, which is larger than $\sqrt{n \log n}$ by a factor of $\log n$. But the bound is $O(\sqrt{n \log n})$, so we need the selection on the small set to be $O(M)$, not $O(M \log M)$. We can use a linear-time selection algorithm like median-of-medians (worst-case linear) or Quickselect (average linear). Since we only care about average-case, Quickselect on the small set gives $O(M)$ expected comparisons. So that's fine.

Now, how to find $k_1$ and $k_2$ with $n + t + O(\sqrt{n \log n})$? Maybe we can find them by a single pass that maintains the top $k_2$ elements? If we maintain the top $k_2$ elements in a sorted list, the expected number of insertions is $k_2 \ln(n/k_2)$, and each insertion takes $\log k_2$ comparisons. Total $n + k_2 \ln(n/k_2) \log k_2$. For $k_2 = t + \sqrt{t \ln n}$, this is $n + (t + \sqrt{t \ln n}) \ln(n/t) \log t$. This is not $n + t$ unless $t$ is very small. So not that.

Maybe the algorithm uses a "binary search" on the value by comparing with a pivot? That's Quickselect. Quickselect average is $2n$. To get $n + t$, we need to reduce the constant factor from 2 to 1 for the $n$ term. How can we do that? By not partitioning the whole array? Quickselect partitions the whole array at each step, costing $n$ comparisons per step. If we only do one partition, we get $n$ comparisons plus recursion on a subarray. The subarray size must be $o(n)$ for the total to be $n + o(n)$. To achieve $n + t$, we need the subarray size to be about $t$? But $t$ can be as large as $n/2$, so subarray size $n/2$ would give recursion cost $n/2 + ...$, total $1.5n + ...$, which is $n + t$! Yes! If we do one partition with a good pivot, we reduce the problem to a subarray of size at most $t$ (or $n-t$). Then the total comparisons would be $n$ (for partitioning) plus the cost to select within the subarray. If the subarray size is $t$, and we find the $t'$th element within it, the cost could be $t + o(t)$? But we need $n + t + o(n)$. If we do one partition and then recursively select in the subarray of size $t$, the total expected comparisons might be $n + t + o(n)$. Let's check: In Quickselect, we partition the whole array (cost $n-1$), then recurse on the appropriate side. The size of the side we recurse on is random. The expected size of the larger side is about $3n/4$ if we choose a random pivot? Actually, the expected size of the subarray is $\max(k, n-k)$? No, if we pick a random pivot, its rank is uniformly distributed. The expected size of the subarray we recurse on is $\sum_{i=1}^n \frac{1}{n} \max(i-1, n-i)$? That's about $3n/4$. That gives $n + 3n/4 + ... = 4n$, not $2n$? Wait, the standard Quickselect expected comparisons is $2n$. The recurrence: $C(n) = n-1 + \frac{1}{n} \sum_{i=1}^n C(\max(i-1, n-i))$? No, if we want the $k$th, we recurse on the left if $i > k$, size $i-1$, or on the right if $i < k$, size $n-i$. The expected size is $\frac{1}{n} \sum_{i=1}^k (n-i) + \frac{1}{n} \sum_{i=k+1}^n (i-1)$. For $k = n/2$, this is about $n/2$. So expected size is $n/2$, giving $n + n/2 + n/4 + ... = 2n$. So the constant 2 comes from the geometric series of expected sizes. To get $n + t$, we need the expected size of the subarray to be $t$? But $t$ is the rank we are looking for. If we choose a pivot that is guaranteed to be near the $t$th element, the subarray size will be small. If we choose a pivot from a sample, we can make the pivot's rank close to $t$ with high probability. Then the subarray size is small, and we only do one partition of the whole array, plus a small recursive selection. The total expected comparisons would be $n + O(\text{subarray size}) + O(\text{sample sort})$. If we choose sample size $s = \sqrt{n \ln n}$, the pivot's rank is within $O(n/\sqrt{s}) = O(\sqrt{n \ln n})$ of the true $t$? Let's compute: We want to find the $t$th largest. Take a sample of size $s$. Find the element in the sample that has rank $r \approx s \cdot t/n$. Use it as pivot. The true rank of this pivot is a random variable with mean $t$ and standard deviation $O(n/\sqrt{s})$. To get the subarray size $O(\sqrt{n \ln n})$, we need $n/\sqrt{s} = O(\sqrt{n \ln n}) \implies \sqrt{s} = \Omega(\sqrt{n/\ln n}) \implies s = \Omega(n/\ln n)$. If $s = n/\ln n$, then sorting sample costs $O(n)$, and partitioning costs $n$. Total $O(n)$. Then the subarray size is $O(\sqrt{n \ln n})$. Then we need to find the $t'$th element in this subarray. The cost to do that could be $O(\sqrt{n \ln n} \log n)$ if we sort, or $O(\sqrt{n \ln n})$ if we use Quickselect. That gives total $n + O(\sqrt{n \ln n})$. But the bound is $n + t + O(\sqrt{n \ln n})$. For $t = n/2$, $n + t = 1.5n$, but this algorithm gives $n + O(\sqrt{n \ln n})$, which is better! So it works for $t = n/2$? But the bound is an upper bound, so $n + O(\sqrt{n \ln n}) \le n + t + O(\sqrt{n \ln n})$ for $t \le n/2$. So this algorithm would prove the bound! But is it valid for all $t$? If $t$ is small, say $t=1$, then $n + O(\sqrt{n \ln n})$ is worse than $n-1$, but it's still an upper bound. So the algorithm: take a sample of size $s = n/\ln n$, find the appropriate pivot, partition, then select in the small subarray. This would give $n + O(\sqrt{n \ln n})$ expected comparisons, which is $\le n + t + O(\sqrt{n \ln n})$ for $t \le n/2$. But the hint says to find both the $k_1$th and $k_2$th elements. The algorithm I just described finds only the $t$th element. The hint specifically says to find the $k_1$th and $k_2$th. Maybe the algorithm is slightly different: we find two pivots that bracket the $t$th element, and then we don't need to recurse on the whole array again; we just need to select from the small middle part. That would also give $n + O(\sqrt{n \ln n})$ comparisons. But the bound is $n + t + O(\sqrt{n \ln n})$. For $t$ small, $n + t$ is larger than $n + O(\sqrt{n \ln n})$? Actually, for $t=1$, $n + t = n+1$, and $n + O(\sqrt{n \ln n})$ is larger for large $n$. So $n + O(\sqrt{n \ln n})$ is not $\le n + 1$ for large $n$. Wait, $O(\sqrt{n \ln n})$ grows without bound, so it's larger than any constant. So $n + O(\sqrt{n \ln n})$ is asymptotically larger than $n + t$ if $t$ is constant. But the bound is $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$. This means there exists a constant $C$ such that $\overline{V}_t(n) \le n + t + C \sqrt{n \log n}$ for all $n$ and $t \le \lceil n/2 \rceil$. If an algorithm gives $n + O(\sqrt{n \log n})$, then for constant $t$, $n + O(\sqrt{n \log n})$ is not $\le n + t + C \sqrt{n \log n}$ because the left side has a constant factor times $\sqrt{n \log n}$ that might be larger than $C$? Actually, if an algorithm gives $n + C_1 \sqrt{n \log n}$, then for $t=1$, we need $n + C_1 \sqrt{n \log n} \le n + 1 + C \sqrt{n \log n}$. This is true if $C \ge C_1$. So $n + O(\sqrt{n \log n})$ is a special case of $n + t + O(\sqrt{n \log n})$ because $t \ge 1$, so $n + t + C \sqrt{n \log n} \ge n + 1 + C \sqrt{n \log n}$. If we have an algorithm with $n + C_1 \sqrt{n \log n}$, we can set $C = C_1$ and it satisfies $n + C_1 \sqrt{n \log n} \le n + t + C_1 \sqrt{n \log n}$ since $t \ge 1$. So the algorithm that gives $n + O(\sqrt{n \log n})$ works for all $t$? But wait, the bound is $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$. For $t = \Theta(n)$, $n + t$ is $\Theta(n)$, so the $O(\sqrt{n \log n})$ is negligible. For $t$ small, $n + t$ is $n + O(1)$, and the bound allows an extra $O(\sqrt{n \log n})$. So any algorithm that achieves $n + O(\sqrt{n \log n})$ automatically achieves $n + t + O(\sqrt{n \log n})$ because $t \ge 1$ and we can absorb the constant. But the hint suggests finding both $k_1$ and $k_2$. Why would we need to find two order statistics if we can just find $t$ directly with $n + O(\sqrt{n \log n})$? Maybe the algorithm that finds $t$ directly with $n + O(\sqrt{n \log n})$ is not obvious, and the hint's approach is to find two bracketing order statistics, which together give a tight bound that works for all $t$. But if we can find $t$ with $n + O(\sqrt{n \log n})$, we are done. Is it known that the average number of comparisons to find the $t$th largest is $n + t + O(\sqrt{n \log n})$? The Floyd-Rivest algorithm gives $n + t + O(n^{2/3} \log^{1/3} n)$. The $O(\sqrt{n \log n})$ bound is tighter and might be achievable by a different algorithm, perhaps using a sample of size $\sqrt{n \ln n}$ and a different strategy.

Let's search my memory for "n + t + O(sqrt(n log n))" in selection. I recall a paper by "Kirchhoff and Stoimenova" or "Karloff"? There's a result by "Mahmoud, Modarres, and Smythe" (1995) "Analysis of Quickselect" that gives the expected number of comparisons as $2n + o(n)$ for Quickselect. The Floyd-Rivest algorithm gives $n + \min(k, n-k) + o(n)$. The $o(n)$ term in Floyd-Rivest is $O(n^{2/3} \log^{1/3} n)$. I think there is a later improvement by "Karloff and Raghavan" or "Karp and Raghavan"? Maybe the $O(\sqrt{n \log n})$ bound is from a paper by "Floyd and Rivest" themselves? In their 1975 paper "Expected time bounds for selection", they give an algorithm with expected comparisons $n + \min(k, n-k) + O(n^{2/3} \log^{1/3} n)$. They also mention that if one only wants to find the median, a simpler algorithm with $O(\sqrt{n \log n})$ error term exists? Not sure.

Wait, the hint says: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." This is a specific construction. Let's try to design an algorithm that finds both $k_1$ and $k_2$ with $n + t + O(\sqrt{n \log n})$ comparisons.

Idea: Use a single pivot that splits the array into three parts: less than $p$, equal? No, we want two pivots. But we can find the two pivots by taking a sample and then partitioning the whole array with respect to those two pivots. That would cost $2n$ comparisons to compare each element to both pivots. To get $n + t$, we need to reduce the number of comparisons. How can we partition with two pivots using only $n + o(n)$ comparisons? We can first compare each element to the smaller pivot? If we choose the two pivots such that one is the $k_1$th largest and the other is the $k_2$th largest in the sample, then in the whole array, most elements will be either larger than both or smaller than both? Actually, $k_1 < k_2$, so the $k_1$th largest is larger than the $k_2$th largest. So the pivot $p_1$ (for $k_1$) is larger than $p_2$ (for $k_2$). If we compare an element $x$ to $p_1$ first: if $x > p_1$, we know it's larger than both. If $x < p_2$, we know it's smaller than both. If $p_2 \le x \le p_1$, it's in the middle. The number of comparisons per element is 2 in the worst case, but on average we can maybe do it in 1? If the middle region is small, then most elements will be resolved after one comparison? If we choose the pivots such that the middle region has size $O(\sqrt{t \ln n})$, then most elements are either $> p_1$ or $< p_2$. If we compare to $p_1$ first, then for elements $> p_1$ we stop after 1 comparison. For elements $< p_1$, we need a second comparison to $p_2$ to see if they are $< p_2$ or in the middle. The probability that an element is $> p_1$ is about $k_1/n$. The probability it is $< p_1$ is about $1 - k_1/n$. So expected comparisons per element = $1 + (1 - k_1/n) = 2 - k_1/n$. Total expected comparisons = $n(2 - k_1/n) = 2n - k_1$. For $k_1 \approx t - \sqrt{t \ln n}$, this is about $2n - t$. That's $2n$, not $n + t$. So that doesn't work.

What if we compare to the median of the two pivots? Not sure.

Maybe we don't partition the whole array with respect to both pivots. Instead, we find the $k_1$th largest by an algorithm that takes $n + k_1$ comparisons, and then we find the $k_2$th largest among the remaining elements by an algorithm that takes $(n - k_1) + (k_2 - k_1)$ comparisons? But as we saw, the first step would be $n + k_1$, the second step would be $(n - k_1) + (k_2 - k_1) = n - 2k_1 + k_2$. Sum = $2n - k_1 + k_2$. Not $n + t$.

Wait, maybe we can find the $k_1$th and $k_2$th simultaneously by a single pass that maintains a set of size $k_2$ of the largest elements? That would be $n + O(k_2 \log k_2)$? Not $n + t$.

Let's think about the structure of the problem. The exercise is from TAOCP, which often uses "adversary" arguments and decision trees. The average-case $\overline{V}_t(n)$ might be defined as the minimum expected number of comparisons over all deterministic algorithms, assuming the input is a random permutation. The hint might be suggesting a specific algorithm that uses a "tournament" structure with some sampling.

Let's look at the section context: "tree selection" and "Kislitsyn's theorem". The worst-case bound for finding the $t$th largest is $W_t(n) \le n - t + \sum_{n+1-t < j \le n} \lceil \lg j \rceil$. For $t$ small, this is $n + t \lceil \lg n \rceil$. The average-case bound is much smaller: $n + t + O(\sqrt{n \log n})$. This suggests that on average, we can find the $t$th largest with only $n + t$ comparisons plus a small error. How? Perhaps by using the fact that in a random permutation, the top $t$ elements are easily identified by a linear scan with a "candidate" set?

Consider the following algorithm for finding the $t$th largest (assuming $t \le n/2$):

  1. Initialize an empty set $S$.
  2. For each element $x$ in the permutation:
    • If $|S| < t$, insert $x$ into $S$ (maintain sorted order).
    • Else if $x > \min(S)$, replace $\min(S)$ with $x$ and re-insert to maintain order.
    • Else ignore $x$. At the end, the $t$th largest is $\min(S)$. This is the "top-$t$" algorithm using a sorted list. The number of comparisons: For each insertion, we compare with $\min(S)$ (1 comparison). If $x > \min(S)$, we need to find its position in the sorted list of size $t$. Using binary search, that takes $\lceil \log_2 t \rceil$ comparisons. If $x \le \min(S)$, we just do 1 comparison. The expected number of elements that are inserted into $S$ (i.e., are among the top $t$) is exactly $t$? Actually, in a random permutation, the first $t$ elements are always inserted. After that, the probability that the $i$th element ($i > t$) is among the top $t$ of the first $i$ elements is $t/i$. So the expected number of insertions is $t + \sum_{i=t+1}^n t/i \approx t(1 + \ln(n/t))$. Each insertion takes $\log t$ comparisons for binary search, plus 1 for the initial comparison. The elements not inserted take 1 comparison. Total expected comparisons $\approx (n - \text{expected insertions}) \cdot 1 + \text{expected insertions} \cdot (1 + \log t) = n + \text{expected insertions} \cdot \log t \approx n + t \ln(n/t) \log t$. This is not $n + t$ unless $t$ is very small (like constant). For $t = n/2$, this is $n + (n/2) \ln 2 \log(n/2) = n + \Omega(n \log n)$, which is much larger than $n + t + O(\sqrt{n \log n})$.

So the simple "top-$t$" algorithm doesn't work for large $t$. The Floyd-Rivest algorithm is more sophisticated. The hint mentions $\sqrt{t \ln n}$, which looks like a standard deviation of a binomial distribution. Perhaps the algorithm uses a sample to estimate the rank of the $t$th element, then only does detailed comparisons on a small subset.

Let's try to design an algorithm based on the hint. We want to find the $k_1$th and $k_2$th largest. Let $k_1 = t - \sqrt{t \ln n}$, $k_2 = t + \sqrt{t \ln n}$. We can use the following approach:

  1. Take a random sample of size $s = \sqrt{n \ln n}$? Or maybe $s = n / \sqrt{t \ln n}$? Let's see.
  2. Sort the sample completely. (Cost $s \log s$).
  3. Use the sample to select two pivots $p$ and $q$ such that $p$ is the $r_1$th largest in the sample, $q$ is the $r_2$th largest, with $r_1$ and $r_2$ chosen so that with high probability, the true $k_1$th and $k_2$th elements lie between $p$ and $q$? Actually, we want to find the exact $k_1$th and $k_2$th, not just bracket them. The hint says "find both the $k_1$th and $k_2$th elements". So we need exact ranks.

Maybe the algorithm is:

  • Find the $k_1$th largest element using an algorithm that takes $n + k_1 + O(\sqrt{n \log n})$ comparisons.
  • Then, since the $k_2$th largest is among the elements smaller than the $k_1$th largest, and the number of such elements is $n - k_1$, we can find the $k_2$th largest by selecting the $(k_2 - k_1)$th largest among those $n - k_1$ elements. But as we saw, that would cost $(n - k_1) + (k_2 - k_1)$ if we use the same algorithm, totaling $2n - 2k_1 + k_2$. Not good.

What if we find the $k_2$th largest first, then the $k_1$th largest is among the elements larger than $k_2$th largest? Same issue.

Maybe we can find both by a single pivot that partitions the array, and then we find the $k_1$th in one side and $k_2$th in the other? If we choose a pivot that is the $k_1$th largest, then the $k_2$th is in the smaller side? No, if pivot is $k_1$th largest, then elements larger than pivot are $k_1-1$ elements. The $k_2$th is smaller, so it's in the larger side (size $n - k_1$). So we still have a large subproblem.

What if we choose a pivot that is the $t$th largest? Then $k_1$ and $k_2$ are in the two sides? If pivot is exactly $t$th, then $k_1$ is in the larger side (elements larger than pivot), size $t-1$, and $k_2$ is in the smaller side (elements smaller than pivot), size $n-t$. We would then need to find the $k_1$th in the larger side (size $t-1$) and the $k_2$th in the smaller side (size $n-t$). But we don't know the exact $t$th element.

Perhaps the algorithm is:

  1. Take a random sample of size $s$. Find the $r$th largest in the sample, where $r \approx s \cdot t/n$. Use this as pivot.
  2. Partition the whole array around this pivot. (Cost $n-1$ comparisons).
  3. Let the rank of the pivot be $r_p$. If $r_p$ is between $k_1$ and $k_2$, then we have bracketed the $t$th element? Actually, if $r_p$ is between $k_1$ and $k_2$, then the $t$th element is either in the left or right side. But we still need to find the exact $t$th element. The hint says find $k_1$th and $k_2$th. If the pivot's rank $r_p$ is exactly $k_1$, then we have found one. But we need both.

Maybe we can use two pivots from the sample. Let's think about the expected number of comparisons if we use a single pivot and then recursively find the $t$th element. The standard Quickselect expected comparisons is $2n$. To reduce it to $n + t$, we need the expected size of the recursive subproblem to be $t$ instead of $n/2$. How can we make the subproblem size $t$? By choosing a pivot that is guaranteed to be close to the $t$th element. If we choose the pivot as the median of a sample of size $s$, the pivot's rank is within $O(n/\sqrt{s})$ of $t$. To get the subproblem size $O(\sqrt{n \log n})$, we need $n/\sqrt{s} = O(\sqrt{n \log n}) \implies s = \Omega(n/\log n)$. If $s = n/\log n$, then sorting the sample costs $O(n)$, partitioning costs $n$, and the subproblem size is $O(\sqrt{n \log n})$. Then we recursively select in the subproblem. The total expected comparisons = $n + O(n) + O(\sqrt{n \log n}) = O(n)$. But we need $n + t + O(\sqrt{n \log n})$. For $t = n/2$, this is $1.5n$, and $O(n)$ is fine. For $t$ small, $O(n)$ might be larger than $n + t + O(\sqrt{n \log n})$? $O(n)$ could be $Cn$, and $n + t + O(\sqrt{n \log n})$ is $n + o(n)$. If $C > 1$, then $Cn$ is not $\le n + t + O(\sqrt{n \log n})$ for large $n$. So the constant factor on $n$ must be 1. The algorithm must have $n + o(n)$ comparisons, not $2n$ or $1.5n$ for large $t$? Wait, for $t = n/2$, $n + t = 1.5n$. So the total comparisons can be up to $1.5n + o(n)$. So a constant factor of 1.5 on $n$ is allowed. The Floyd-Rivest algorithm achieves $n + t + o(n)$, which for $t=n/2$ is $1.5n + o(n)$. So the constant on $n$ is exactly 1 for the first $n$ term, and the $t$ term is added. In the Floyd-Rivest algorithm, the total expected comparisons is $n + \min(k, n-k) + o(n)$. For $k = t \le n/2$, it's $n + t + o(n)$. So the $n$ term has coefficient 1, and the $t$ term has coefficient 1. The $o(n)$ term is $O(n^{2/3} \log^{1/3} n)$. The exercise asks for $O(\sqrt{n \log n})$, which is a better (smaller) $o(n)$ term. So the Floyd-Rivest algorithm does not achieve this bound, but there might be a simpler algorithm that achieves $O(\sqrt{n \log n})$ error term.

How to achieve $O(\sqrt{n \log n})$ error? Perhaps by using a sample of size $\sqrt{n \log n}$ and doing a different kind of selection. Let's analyze the sample-based algorithm with two pivots.

Consider the following algorithm for finding the $t$th largest (for $t \le n/2$):

  1. Choose a random sample of size $s = \lceil \sqrt{n \ln n} \rceil$.
  2. Sort the sample completely.
  3. Let $p$ be the element in the sample with rank $r_1 = \lfloor s \cdot t/n \rfloor$? Actually, we want to bracket the $t$th element. We can choose two elements from the sample: one with rank $r_1 = \lfloor s \cdot (t - \sqrt{t \ln n})/n \rfloor$ and another with rank $r_2 = \lceil s \cdot (t + \sqrt{t \ln n})/n \rceil$. But we don't know the exact ranks of these sample elements in the whole array without comparing them to all other elements.
  4. Instead, we can use the sample to partition the whole array. Compare every element of the array to the two chosen pivots? That would be $2n$ comparisons. To reduce to $n + t$, we need a more efficient partitioning.

Wait, maybe the algorithm is not sample-based. Maybe it's a "tournament" algorithm that on average performs well. For example, we can find the maximum in $n-1$ comparisons. To find the $t$th largest, we could find the maximum, then the second maximum, etc., but we don't need to do full tournament each time. There's an algorithm by "Schönheim" that finds the $t$th largest with $n + t \log n$ worst-case. The average-case of that algorithm might be $n + t + O(\sqrt{n \log n})$? Let's check: In the tournament tree, to find the maximum, we build a binary tree with $n$ leaves. The maximum is the root. The second maximum is found by examining the path of the maximum, which takes $\lceil \log n \rceil - 1$ comparisons. The third maximum? We would need to examine the paths of the first two. The total comparisons to find the first $t$ elements is $n - 1 + \sum_{i=1}^{t-1} \lceil \log i \rceil$? That's worst-case. The average-case of this tournament method: The tournament tree is built once, costing $n-1$ comparisons. Then we find the second, third, etc. by running "replacement" tournaments. The number of comparisons to find the $i$th largest given the first $i-1$ is the depth of the tree minus something? Actually, in a knockout tournament, after finding the maximum, the second maximum is among the opponents of the maximum. We can find it by comparing those opponents. The number of comparisons to find the second is $\lceil \log n \rceil - 1$. For the third, we need to find the maximum among the remaining opponents? This is the "tree selection" algorithm. The worst-case for finding the $t$th largest is $n + t \log n$ (approximately). The average-case might be better because the tournament tree is built on a random permutation? The structure of the tournament tree is fixed, but the input order is random. The number of comparisons to find the $t$th largest might be $n + t + O(\sqrt{n \log n})$ on average? Let's test for $t=1$: $n-1$, matches $n + 1 + O(...)$? For $t=2$: worst-case is $n + \lceil \log n \rceil - 1$. Average-case? The tournament tree is a binary tree. The maximum is at the root. The second maximum is the maximum of the opponents along the path. The number of opponents is the height of the tree, which is $\lceil \log n \rceil$. We need to find the maximum among them. The expected number of comparisons to find the maximum of a random permutation of size $h$ is $h-1$. So expected comparisons for second = $h-1 = \lceil \log n \rceil - 1$. That's still $\log n$, not $O(1)$. So for $t=2$, expected comparisons is $n + \log n$, which is larger than $n + t + O(\sqrt{n \log n}) = n + 2 + O(\sqrt{n \log n})$. For large $n$, $\log n$ is smaller than $\sqrt{n \log n}$, so $n + \log n \le n + 2 + C \sqrt{n \log n}$ holds for sufficiently large $C$. So the bound allows $\log n$ for small $t$. For $t = \log n$, the tournament method would cost $n + t \log n = n + \log^2 n$, while the bound is $n + \log n + O(\sqrt{n \log n})$, which is larger. So the tournament method might actually satisfy the bound? Let's check: The worst-case for finding the $t$th largest using tree selection is $W_t(n) \le n - t + \sum_{j=n+1-t}^n \lceil \lg j \rceil$. For $t \le n/2$, this is at most $n + t \lceil \lg n \rceil$. The average-case $\overline{V}t(n)$ is the minimum over all algorithms. The tournament algorithm is one algorithm. Its average number of comparisons might be the same as worst-case because the decision tree is fixed? Actually, the tournament algorithm's comparisons are deterministic given the outcomes. The number of comparisons might depend on the outcomes. For finding the second largest, the algorithm does: build the tournament (fixed $n-1$ comparisons), then find the max among the opponents of the overall max. The opponents are the elements that lost to the champion. The number of opponents is the height of the tree, which is fixed. Finding the maximum among them takes $h-1$ comparisons, regardless of their values. So the total is fixed: $n - 1 + h - 1 = n + h - 2$. So the average-case of this algorithm is the same as worst-case: $n + \lceil \log n \rceil - 2$. For $t=2$, the bound is $n + 2 + O(\sqrt{n \log n})$. Since $\log n \le O(\sqrt{n \log n})$ for large $n$, the tournament algorithm satisfies the bound for $t=2$. For larger $t$, the tournament algorithm's comparisons are $n + \sum{i=1}^{t-1} \lceil \log i \rceil$? Actually, Kislitsyn's bound is $n - t + \sum_{j=n+1-t}^n \lceil \lg j \rceil$. For $t$ up to $n/2$, this is roughly $n + t \log n$. The average-case of this specific algorithm might be the same as worst-case. But the bound we need to prove is $n + t + O(\sqrt{n \log n})$. For $t = n/2$, the tournament algorithm gives $n + (n/2) \log n$, which is much larger than $1.5n$. So the tournament algorithm does NOT satisfy the bound for large $t$. So we need a different algorithm.

The bound $n + t + O(\sqrt{n \log n})$ is sub-quadratic in $t$. The Floyd-Rivest algorithm gives $n + t + O(n^{2/3} \log^{1/3} n)$. For $t = n/2$, this is $1.5n + O(n^{2/3} \log^{1/3} n)$. The bound in the exercise is $1.5n + O(\sqrt{n \log n})$. Since $\sqrt{n \log n}$ grows slower than $n^{2/3} \log^{1/3} n$, the exercise's bound is stronger. Is there an algorithm that achieves $O(\sqrt{n \log n})$? Maybe the algorithm uses a sample of size $O(\sqrt{n \log n})$ and does something clever. Let's try to design an algorithm with error $O(\sqrt{n \log n})$.

Suppose we take a sample of size $s = \sqrt{n \ln n}$. Sort it. We want to find the $t$th largest. We can use the sample to estimate the rank. Specifically, we can find the element in the sample that is the $r$th largest, where $r \approx s \cdot t/n$. Let this element be $p$. We then compare every element of the array to $p$? That would be $n$ comparisons. The rank of $p$ in the whole array is a random variable. Its expected value is $t$. The standard deviation is $O(n/\sqrt{s}) = O(\sqrt{n \ln n})$. So with high probability, the rank of $p$ is within $O(\sqrt{n \ln n})$ of $t$. If we partition the array around $p$, we get a subarray of size $O(\sqrt{n \ln n})$ that contains the $t$th element. Then we can find the $t$th element within that subarray by sorting it or using Quickselect. The total comparisons = sorting sample ($s \log s$) + partitioning ($n$) + sorting subarray ($O(\sqrt{n \ln n} \log n)$). The dominant terms are $n + O(\sqrt{n \ln n} \log n)$. But $O(\sqrt{n \ln n} \log n) = O(\sqrt{n} \log^{3/2} n)$, which is larger than $O(\sqrt{n \log n})$. To get $O(\sqrt{n \log n})$, we need the subarray sorting cost to be $O(\sqrt{n \ln n})$, not $O(\sqrt{n \ln n} \log n)$. We can achieve $O(\sqrt{n \ln n})$ by using a linear-time selection algorithm on the subarray, like median-of-medians (worst-case linear) or Quickselect (average linear). Quickselect on a set of size $M$ takes $O(M)$ expected comparisons. So the expected cost for the subarray is $O(\sqrt{n \ln n})$. The expected cost of sorting the sample is $s \log s = \sqrt{n \ln n} \cdot \frac{1}{2} \ln n = \frac{1}{2} \sqrt{n} (\ln n)^{3/2}$, which is $O(\sqrt{n} \ln^{3/2} n)$. That's larger than $O(\sqrt{n \ln n})$! So the sample sorting cost dominates and is too large. We need the sample sorting cost to be $O(\sqrt{n \ln n})$ as well. To achieve that, we need $s \log s = O(\sqrt{n \ln n})$. If $s = \sqrt{n \ln n}$, then $s \log s = \sqrt{n \ln n} \cdot \frac{1}{2} \ln n = \frac{1}{2} \sqrt{n} \ln^{3/2} n$, which is $\omega(\sqrt{n \ln n})$. To make $s \log s = O(\sqrt{n \ln n})$, we need $s$ to be smaller, like $s = \sqrt{n \ln n} / \log n$? That would reduce the sample size, but then the standard deviation $n/\sqrt{s}$ would increase, making the subarray larger. There's a trade-off.

Let's solve for $s$ such that both $s \log s$ and $n/\sqrt{s}$ are $O(\sqrt{n \log n})$. We want $n/\sqrt{s} \le C \sqrt{n \log n} \implies \sqrt{s} \ge \sqrt{n / \log n} \implies s \ge n / \log n$. And we want $s \log s \le C \sqrt{n \log n}$. If $s = n / \log n$, then $s \log s \approx n$, which is much larger than $\sqrt{n \log n}$. If $s = \sqrt{n \log n}$, then $s \log s \approx \sqrt{n} \log^{3/2} n$, which is larger than $\sqrt{n \log n}$. So we cannot achieve both with a single sample and single partition. The Floyd-Rivest algorithm uses $s = n^{2/3} \log^{1/3} n$, giving $s \log s \approx n^{2/3} \log^{4/3} n$ and $n/\sqrt{s} \approx n^{2/3} \log^{-1/6} n$, both $O(n^{2/3} \text{polylog})$. The error term is $O(n^{2/3} \log^{1/3} n)$? Actually, their error is $O(n^{2/3} \log^{1/3} n)$. So they balance the two costs to get $n^{2/3}$. To get $\sqrt{n}$, we need a different approach.

Maybe the algorithm does not use a sample. The hint mentions finding the $k_1$th and $k_2$th elements. Perhaps we can find them by a "tournament" that maintains a set of candidates of size $O(\sqrt{t \log n})$?

Another idea: The bound $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$ might be proven by a non-constructive argument using the probabilistic method or by analyzing the optimal decision tree? But the hint says "Show that with this many comparisons we can in fact find both...", so it's constructive.

Let's re-read the hint: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." This is a very specific hint. It suggests an algorithm that finds two specific order statistics. The indices are $t \pm \sqrt{t \ln n}$. The gap is $2\sqrt{t \ln n}$. This gap size is exactly the typical standard deviation of a binomial distribution. This suggests that if we have a set of elements and we want to find the $t$th largest, we can first find an element that is approximately the $t$th largest, and then we only need to resolve a small neighborhood.

Maybe the algorithm is:

  1. Find the $k_1$th largest element using an algorithm that takes $n + k_1$ comparisons? But we don't have such an algorithm yet.
  2. Alternatively, we can find both by a single pass that maintains a "window" of size $O(\sqrt{t \ln n})$ around the $t$th element. How? We can keep track of the top $k_1$ elements and the bottom $n-k_2$ elements? That's a lot.

Wait, maybe we can use the following approach:

  • First, find the $k_1$th largest element by doing a tournament that only keeps track of the top $k_1$ elements? No, finding the $k_1$th largest with $n + k_1$ comparisons is not trivial. There is a known algorithm by "Floyd and Rivest" that does exactly that. But the exercise is from TAOCP, and Knuth might have a different algorithm in mind.

Let's search my memory for "n + t + O(sqrt(n log n))" in Knuth's exercises. I recall that in TAOCP Vol 3, there is an exercise about the average number of comparisons for selection. The answer might be in the back of the book. The hint is given. The exercise number is 24. The hint says: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." This is likely a reference to an algorithm by "Kislitsyn" or "Schreier"? Or maybe it's an algorithm by "Kirkpatrick"? The solution" something.

Let's think about the phrase "easily located". If we have the $k_1$th and $k_2$th elements, we know their values. The $t$th element is between them. The number of elements between them is $k_2 - k_1 + 1 = O(\sqrt{t \ln n})$. We can find the $t$th element by doing a binary search among these elements? Or by running a selection algorithm on them. The cost to select among $M = O(\sqrt{t \ln n})$ elements is $O(M) = O(\sqrt{n \log n})$ on average. So the "easily located" part is clear.

Now, how to find $k_1$th and $k_2$th with $n + t + O(\sqrt{n \log n})$ comparisons? Perhaps we can find them simultaneously by doing a single pass that partitions the array into three parts: elements larger than $k_1$th, elements between $k_1$th and $k_2$th, and elements smaller than $k_2$th. But we don't know the values of $k_1$th and $k_2$th. We can use a random sample to estimate them.

Let's consider an algorithm by "Hoare" (Quickselect) but with two pivots. If we choose two pivots that are close to the $k_1$th and $k_2$th ranks, we can partition the array into three parts in one pass. The expected number of comparisons for a three-way partition is $2n$ in the worst case, but we can do it in $n + o(n)$ if the middle part is small? Actually, if we choose pivots $p < q$ (so $p$ is smaller, $q$ is larger), and we want to partition into $<p$, between $p$ and $q$, $>q$. We can do: for each element $x$, compare with $p$ first. If $x < p$, done (1 comparison). Else compare with $q$. If $x > q$, done (2 comparisons). Else it's in the middle (2 comparisons). The expected number of comparisons is $n \cdot (P(x<p) \cdot 1 + P(x \ge p) \cdot 2)$. If $p$ is close to the $k_1$th largest, then $P(x < p)$ is about $(n-k_1)/n$. For $t$ near $n/2$, $k_1 \approx n/2$, so $P(x<p) \approx 1/2$, expected comparisons $\approx 1.5n$. If we instead compare with $q$ first, $P(x>q) \approx k_2/n \approx 1/2$, expected comparisons $\approx 1.5n$. So three-way partition costs about $1.5n$. The total algorithm cost would be $1.5n$ for partitioning plus recursion. If we only do one three-way partition and then sort the middle part, the total is $1.5n + O(\sqrt{n \log n} \log n)$. For $t = n/2$, $n + t = 1.5n$, so this matches! For smaller $t$, $k_1 \approx t$, $P(x<p) \approx (n-t)/n \approx 1$, so expected comparisons $\approx 1 \cdot 1 + 0 \cdot 2 = n$. So three-way partition with $p$ close to $k_1$th and $q$ close to $k_2$th gives expected comparisons $n + t$? Let's check: $p$ is the $k_1$th largest, so $P(x < p) = (n - k_1)/n$. $q$ is the $k_2$th largest, so $P(x > q) = k_2/n$. The expected comparisons if we compare to $p$ first: $n [P(x<p) \cdot 1 + P(x \ge p) \cdot 2] = n [1 + P(x \ge p)] = n + n \cdot P(x \ge p) = n + k_1$. Because $P(x \ge p) = k_1/n$. So expected comparisons = $n + k_1$. If we instead compare to $q$ first: expected comparisons = $n + n \cdot P(x \le q) = n + (n - k_2)$. Since $k_2 = t + \sqrt{t \ln n}$, $n - k_2 = n - t - \sqrt{t \ln n}$. For $t \le n/2$, $n - k_2 \ge t - \sqrt{t \ln n} \approx k_1$. So the expected comparisons is about $n + k_1$ or $n + (n - k_2)$. To minimize, we should compare to the pivot that is more likely to be extreme? If we want to find the $k_1$th and $k_2$th, we can do a three-way partition with $p$ and $q$. But we don't know $p$ and $q$ beforehand. We need to find them.

Wait! This is a key insight: If we have two pivots $p$ and $q$ with $p$ being the $k_1$th largest and $q$ being the $k_2$th largest, then partitioning the array with respect to $p$ and $q$ costs exactly $n + k_1$ comparisons if we compare to $p$ first, or $n + (n - k_2)$ if we compare to $q$ first. Since $k_1 \approx t - \sqrt{t \ln n}$ and $k_2 \approx t + \sqrt{t \ln n}$, both are close to $t$ when $t$ is not too large? For $t \le n/2$, $k_1 \le t$, so $n + k_1 \le n + t$. So if we can find such pivots $p$ and $q$ without too many extra comparisons, and then partition, we get the three sets. Then we already know the $k_1$th and $k_2$th elements? No, we know $p$ and $q$ are the $k_1$th and $k_2$th? If we choose $p$ and $q$ to be the exact $k_1$th and $k_2$th largest elements, then partitioning confirms their ranks and separates the rest. But we don't have them yet.

What if we use a random sample to get candidates for $p$ and $q$? We could take a sample, find the $r_1$th and $r_2$th largest in the sample, and use them as $p$ and $q$. Their ranks in the whole array will be approximately $k_1$ and $k_2$. Then we partition the whole array with respect to these $p$ and $q$. The partition will give us three sets: $L$ (elements $> p$), $M$ (elements between $p$ and $q$), $R$ (elements $< q$). The sizes of $L$, $M$, $R$ will be close to $k_1-1$, $k_2 - k_1 - 1$, $n - k_2$. The exact $k_1$th and $k_2$th elements are in $M$ (or are $p$ and $q$ themselves). Then we can find the exact $k_1$th and $k_2$th elements by selecting within $M \cup {p,q}$. The size of $M$ is about $k_2 - k_1 = 2\sqrt{t \ln n}$. So we need to select the $k_1 - |L|$th element from $M$, etc. The cost of selecting within $M$ is $O(\sqrt{t \ln n})$ expected comparisons.

Now, what is the total cost?

  • Choose sample of size $s$, sort it: $O(s \log s)$.
  • Partition the whole array with respect to $p$ and $q$: expected $n + \min(k_1, n - k_2)$ comparisons? Actually, we can choose the order of comparison to minimize expected comparisons. If $k_1 \le n - k_2$, we compare to $p$ first, cost $n + k_1$. If $n - k_2 < k_1$, we compare to $q$ first, cost $n + (n - k_2)$. Since $t \le n/2$, $k_2 \le n/2 + \sqrt{t \ln n}$. So $n - k_2 \ge n/2 - \sqrt{t \ln n}$. For $t$ small, $k_1 \approx t$ is small, so $n + k_1$ is good. For $t$ near $n/2$, $n - k_2 \approx n/2$, $k_1 \approx n/2$, both give $n + n/2 = 1.5n$. So the partition cost is $n + \min(k_1, n - k_2) \le n + t$ (since $k_1 \le t$ and $n - k_2 \le n - t \le n/2$, but we need to ensure it's $\le n + t$? Actually, $n - k_2$ could be larger than $t$ if $t$ is small? For $t=1$, $k_2 = 1 + \sqrt{\ln n}$, $n - k_2 \approx n$, which is larger than $t=1$. But we can choose the order: compare to $p$ first, cost $n + k_1$. $k_1$ might be negative for $t=1$? The hint uses floor and ceiling, so for small $t$, $k_1$ could be 1? The problem says $t \le \lceil n/2 \rceil$, but doesn't specify lower bound. We can handle small $t$ separately with a simpler algorithm (like linear scan for min). So we can assume $t$ is large enough that $k_1 \ge 1$. Then $\min(k_1, n - k_2) \le t$? For $t \le n/2$, $n - k_2 \ge n/2 - \sqrt{t \ln n}$. This could be larger than $t$ if $t$ is small. For example, $n=100$, $t=10$, $k_2 \approx 10 + \sqrt{10 \ln 100} \approx 10 + \sqrt{46} \approx 16.8$, $n - k_2 \approx 83$, which is larger than $t=10$. So $n - k_2 > t$. Then if we compare to $q$ first, cost is $n + 83$, which is $n + t + 73$, larger than $n + t$. But we can choose to compare to $p$ first, giving cost $n + k_1$. $k_1 \approx 10 - \sqrt{46} \approx 3.2$, so cost $n + 3$, which is $\le n + t$. So we always compare to the pivot that is closer to the extreme? Actually, $p$ is the $k_1$th largest (larger rank), so comparing to $p$ first costs $n + k_1$. $q$ is the $k_2$th largest, comparing to $q$ first costs $n + (n - k_2)$. We want the smaller of $k_1$ and $n - k_2$. Since $k_1 \approx t - \sqrt{t \ln n}$ and $n - k_2 \approx n - t - \sqrt{t \ln n}$. For $t \le n/2$, $t \le n - t$, so $k_1 \le n - k_2$ for large $n$? Actually, $k_1 \le t$, and $n - k_2 \ge n - t - \sqrt{t \ln n}$. Since $t \le n/2$, $n - t \ge t$, so $n - k_2 \ge t - \sqrt{t \ln n} \approx k_1$. It could be that $k_1$ is slightly less than $n - k_2$ or slightly more, depending on $t$. But both are at most $t + O(\sqrt{t \ln n})$. Actually, $k_1 \le t$, and $n - k_2 \le n - t$. Since $t \le n/2$, $n - t \ge t$, so the minimum is at most $t$. So the partition cost is at most $n + t$.

But we still need to get the pivots $p$ and $q$. We get them from the sample. We need the sample to be such that the ranks of $p$ and $q$ in the whole array are exactly $k_1$ and $k_2$? Or we just need them to be close enough that the middle set $M$ is small? The hint says "find both the $k_1$th and $k_2$th elements". So we need the exact $k_1$th and $k_2$th elements. If we pick $p$ and $q$ from the sample, they might not be the exact $k_1$th and $k_2$th. But we can use them to narrow down the search to a small set, and then find the exact ones within that set.

Let's formalize the algorithm:

  1. If $t$ is small (e.g., $t \le \sqrt{n \ln n}$), use a simple algorithm (like top-$t$ or Quickselect) and prove the bound holds. (We can handle this edge case.)
  2. Let $k_1 = \lfloor t - \sqrt{t \ln n} \rfloor$, $k_2 = \lceil t + \sqrt{t \ln n} \rceil$. (Assume $k_1 \ge 1$.)
  3. Choose a random sample $S$ of size $s = \lceil \sqrt{n \ln n} \rceil$? Or maybe $s = \lceil \sqrt{n \ln n} \rceil$? We need to determine $s$.
  4. Sort $S$ completely. (Cost $O(s \log s)$.)
  5. From $S$, select two elements $p$ and $q$ that are the $r_1$th and $r_2$th largest in $S$, where $r_1 \approx s \cdot k_1/n$, $r_2 \approx s \cdot k_2/n$. (These are approximations of the $k_1$th and $k_2$th largest in the whole set.)
  6. Compare every element of the whole array to $p$ and $q$? Wait, we want to partition the array with respect to $p$ and $q$. But we don't know if $p$ and $q$ are exactly the $k_1$th and $k_2$th. If we partition, we get three sets $L = {x > p}$, $M = {x: p \ge x \ge q}$, $R = {x < q}$. The sizes of $L$ and $R$ are random. The true $k_1$th and $k_2$th elements will be in $M$ with high probability if $p$ and $q$ are chosen appropriately. Then we can find the exact $k_1$th and $k_2$th by selecting within $M$. The size of $M$ will be $O(\sqrt{t \ln n})$ with high probability.
  7. The cost of partitioning: we compare each element to $p$ and $q$ in an optimal order. Expected comparisons for partitioning is $n + \min(|L|, |R|)$. Since $|L| \approx k_1$, $|R| \approx n - k_2$, this is $n + \min(k_1, n - k_2) \le n + t$.
  8. The cost of sorting the sample: $s \log s$. We need $s \log s = O(\sqrt{n \log n})$? If $s = \sqrt{n \ln n}$, then $s \log s = \sqrt{n \ln n} \cdot \frac{1}{2} \ln n = \frac{1}{2} \sqrt{n} (\ln n)^{3/2}$, which is $\omega(\sqrt{n \log n})$. So this is too large. We need a smaller sample or a way to find $p$ and $q$ without sorting the entire sample.

Maybe we don't need to sort the whole sample. We only need to find two order statistics of the sample. The expected number of comparisons to find the $r$th order statistic in a sample of size $s$ is $O(s)$ if we use Quickselect. We can find the $r_1$th and $r_2$th in $O(s)$ expected comparisons. Then we don't need to sort the whole sample! We just find two elements in the sample that approximate the $k_1$th and $k_2$th. The cost of finding them in the sample is $O(s)$ expected comparisons. If we set $s = \sqrt{n \ln n}$, then $O(s) = O(\sqrt{n \ln n})$, which fits the bound! And we don't need to sort the whole sample.

But wait: If we find the $r_1$th and $r_2$th in the sample using Quickselect, we need to know the values of $r_1$ and $r_2$. We can choose $r_1 = \lfloor s \cdot k_1/n \rfloor$, $r_2 = \lceil s \cdot k_2/n \rceil$. We find the $r_1$th largest and $r_2$th largest in the sample. These become our pivots $p$ and $q$. Then we partition the whole array with respect to $p$ and $q$. The ranks of $p$ and $q$ in the whole array will be close to $k_1$ and $k_2$ by Chernoff bounds. The middle set $M$ will have size $O(\sqrt{t \ln n})$ with high probability. Then we find the exact $k_1$th and $k_2$th within $M$ by recursively applying the same algorithm? But $M$ is small, so we can just sort it or use a simple selection algorithm. The expected size of $M$ is $k_2 - k_1 + O(\text{deviation})$. The deviation of the rank of $p$ from its expected value is $O(n/\sqrt{s}) = O(\sqrt{n \ln n})$. Since $k_2 - k_1 = 2\sqrt{t \ln n} \le 2\sqrt{n \ln n}$, the size of $M$ is $O(\sqrt{n \ln n})$. The cost to find the exact $k_1$th and $k_2$th within $M$ is $O(|M|)$ expected comparisons if we use Quickselect, which is $O(\sqrt{n \ln n})$.

Total expected comparisons:

  • Find $p$ and $q$ in sample: $O(s) = O(\sqrt{n \ln n})$.
  • Partition whole array: expected $n + \min(k_1, n - k_2) \le n + t$.
  • Find exact $k_1$th and $k_2$th in $M$: $O(\sqrt{n \ln n})$. Total: $n + t + O(\sqrt{n \ln n})$.

This seems to work! The sample size $s = \sqrt{n \ln n}$ is chosen so that the standard deviation of the rank of the sample quantile is $O(\sqrt{n \ln n})$, matching the gap $k_2 - k_1$. The cost of finding the pivots in the sample is $O(s) = O(\sqrt{n \ln n})$. The partition cost is $n + \min(k_1, n - k_2)$. Since $k_1 \le t$ and $n - k_2 \le n - t$, and $t \le n/2$, we have $\min(k_1, n - k_2) \le t$? Wait, for $t$ small, $k_1 \approx t$ is small, but $n - k_2 \approx n$ is large. We can choose the order of partitioning to compare to $p$ first, giving cost $n + |L|$. $|L|$ is the number of elements larger than $p$. The expected $|L|$ is $k_1$. So expected cost $n + k_1 \le n + t$. For $t$ near $n/2$, $k_1 \approx n/2$, $n - k_2 \approx n/2$, both give $n + n/2 = n + t$. So indeed the partition cost is $n + O(t)$. Actually, we need to ensure the expected value of $\min(|L|, |R|)$ is at most $t + O(\sqrt{n \ln n})$. Since $k_1 \le t$, and $|L|$ is close to $k_1$ with high probability, the expected $\min(|L|, |R|)$ is at most $t + O(\sqrt{n \ln n})$. The $O(\sqrt{n \ln n})$ can be absorbed into the $O(\sqrt{n \log n})$ term.

But wait: The partition step requires comparing each element to $p$ and $q$. The expected number of comparisons is $n + \min(|L|, |R|)$ if we compare to the pivot that is more extreme first. But we don't know which pivot is more extreme until we compare them? We can compare $p$ and $q$ first (1 comparison). Then we know which is larger. Since $k_1 < k_2$, $p$ should be larger than $q$ (if the sample estimates are correct). We can compare $p$ and $q$ (cost 1). If $p > q$, then we can partition by comparing each element to $p$ first: if $x > p$, it's in $L$ (1 comparison). Else, compare to $q$: if $x < q$, it's in $R$ (2 comparisons). Else in $M$ (2 comparisons). The expected number of comparisons is $n + |L|$ (since each element not in $L$ requires a second comparison). If $p < q$, we can swap roles. So expected comparisons = $n + \min(|L|, |R|)$. This matches.

Now, we need to ensure that the ranks of $p$ and $q$ are such that $k_1$ and $k_2$ are in $M$ with high probability, and that the size of $M$ is small. By Chernoff bounds, the rank of the $r$th sample quantile has standard deviation $O(n/\sqrt{s})$. With $s = \sqrt{n \ln n}$, the standard deviation is $O(\sqrt{n \ln n})$. The gap between $k_1$ and $k_2$ is $2\sqrt{t \ln n} \le 2\sqrt{n \ln n}$. So the expected deviation is of the same order as the gap. To guarantee that the true $k_1$th and $k_2$th are inside $M$, we might need to widen the gap slightly or handle the small probability of failure by a fallback method. The hint says "find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements". This suggests we can actually find the exact ones with that many comparisons, possibly by a deterministic algorithm or by an algorithm that always succeeds with expected comparisons bounded. In expected value analysis, we can have a small probability of failure that we handle by a slower algorithm, adding negligible expected cost.

Let's check the constants: The hint uses $\sqrt{t \ln n}$ with natural log. The standard deviation is $O(\sqrt{n \ln n})$. The gap is $2\sqrt{t \ln n}$. For $t$ much smaller than $n$, the gap is smaller than the standard deviation. That might be a problem: if $t = \sqrt{n}$, then $\sqrt{t \ln n} = \sqrt{\sqrt{n} \ln n} = n^{1/4} \sqrt{\ln n}$, while $n/\sqrt{s} = \sqrt{n \ln n} = n^{1/2} \sqrt{\ln n}$. The standard deviation is much larger than the gap. So the pivots from a sample of size $\sqrt{n \ln n}$ might not be close enough to $k_1$ and $k_2$ when $t$ is small. In that case, the middle set $M$ might be large. But we can handle small $t$ by a different method. The exercise says $t \le \lceil n/2 \rceil$, but doesn't require $t$ to be large. We can assume $t$ is large enough, or we can choose the sample size adaptively based on $t$. The hint says $\lfloor t - \sqrt{t \ln n} \rfloor$ and $\lceil t + \sqrt{t \ln n} \rceil$. This gap size is $\approx 2\sqrt{t \ln n}$. The standard deviation of the sample quantile is $\approx n/\sqrt{s}$. To make the standard deviation comparable to the gap, we need $n/\sqrt{s} = O(\sqrt{t \ln n})$, i.e., $s = \Omega(n^2/(t \ln n))$. For small $t$, this $s$ could be larger than $n$, which is impossible. So for small $t$, we cannot rely on a single sample to get a gap of size $\sqrt{t \ln n}$ with high probability. We need a different strategy for small $t$.

Maybe the algorithm is different: Instead of using a sample of fixed size, we use a tournament that naturally finds the top elements with few comparisons. The bound $n + t + O(\sqrt{n \log n})$ might be achieved by a simple algorithm: "Find the $t$th largest by doing a binary search on the value?" No.

Let's re-read the hint carefully: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." This is a statement about the existence of an algorithm. It might be a known result from a paper. The use of $\ln n$ suggests a probabilistic analysis. The phrase "easily located" might mean that once we have these two elements, we can find the $t$th by a simple linear scan of the elements between them? But there are $O(\sqrt{t \ln n})$ elements, so scanning them takes $O(\sqrt{t \ln n})$ comparisons, which is $O(\sqrt{n \log n})$.

Perhaps the algorithm to find the two elements is:

  • First, find the $k_1$th largest using an algorithm that takes $n + k_1 + O(\sqrt{n \log n})$ comparisons.
  • Then, from the elements that are smaller than the $k_1$th largest, find the $(k_2 - k_1)$th largest. The number of such elements is $n - k_1$. The rank we need is $k_2 - k_1 = 2\sqrt{t \ln n}$. We can find this by the same algorithm, but the input size is $n - k_1$ and the target rank is small. The expected comparisons for this step would be $(n - k_1) + (k_2 - k_1) + O(\sqrt{(n - k_1) \log(n - k_1)})$. Summing with the first step gives $2n - 2k_1 + k_2 + ...$, which is too large.

But wait, maybe we can find the $k_1$th largest without partitioning the whole array? If we use a tournament to find the $k_1$th largest, the worst-case is $n + k_1 \log n$. The average-case might be $n + k_1 + O(\sqrt{n \log n})$? How? If we build a tournament tree, the comparisons are deterministic. The average-case over random permutations is the same as worst-case for a fixed tournament tree? Actually, the tournament tree's comparisons depend on the outcomes, but the structure of comparisons is fixed by the algorithm. The number of comparisons to find the $k$th largest using the tournament algorithm is deterministic given the permutation? No, the algorithm might make decisions based on outcomes. For example, to find the second largest, we build the tree (cost $n-1$), then we find the maximum among the opponents of the root. The number of comparisons for the second step is fixed (height of tree minus 1). So the total is fixed. The average-case over all permutations is the same as the worst-case for this algorithm. So the tournament algorithm does not achieve $n + t + O(\sqrt{n \log n})$ for large $t$.

So the algorithm must be a different one, likely using randomization and expected value.

Let's search for "n + t + O(sqrt(n log n))" in the context of selection. I recall a paper by "Karloff and Raghavan" (1990) "Randomized algorithms and expected time bounds for selection" maybe? Or "Floyd and Rivest" (1975) gives $n + \min(k, n-k) + O(n^{2/3} \log^{1/3} n)$. The $O(\sqrt{n \log n})$ bound might be from a later paper, or it might be achievable by a simpler algorithm that is not optimal but has this specific bound. The exercise is from TAOCP, and Knuth often includes results from the literature. The hint is very specific: find the $k_1$th and $k_2$th with $k_1 = t - \sqrt{t \ln n}$, $k_2 = t + \sqrt{t \ln n}$. This looks like a result by "Kirchhoff" or "Mahmoud"?

Maybe the algorithm is:

  1. Take a random sample of size $s = \sqrt{n \ln n}$.
  2. Find the $r_1$th and $r_2$th largest in the sample, where $r_1 = \lfloor s \cdot k_1/n \rfloor$, $r_2 = \lceil s \cdot k_2/n \rceil$.
  3. Use these as pivots to partition the whole array.
  4. Recursively find the exact $k_1$th and $k_2$th in the middle part.

The expected size of the middle part is $O(\sqrt{t \ln n} + n/\sqrt{s})$. If we set $s = n / \sqrt{t \ln n}$? Then $n/\sqrt{s} = \sqrt{n \sqrt{t \ln n}}$, which is not matching. Let's solve for $s$ to make the middle part size $O(\sqrt{t \ln n})$. The deviation of the sample quantile is $O(n/\sqrt{s})$. We want $n/\sqrt{s} = O(\sqrt{t \ln n}) \implies s = \Omega(n^2/(t \ln n))$. If $t$ is small, $s$ might exceed $n$. In that case, we can just use $s = n$ (i.e., we have the whole array). If $s = n$, then we are just selecting directly, which would cost $O(n)$? But we need $n + t + O(\sqrt{n \log n})$. If $t$ is small, say $t = O(1)$, then $s = \Omega(n^2/\ln n)$ which is larger than $n$. So we cannot achieve the gap $\sqrt{t \ln n}$ with a sample of size $\le n$ for very small $t$. For $t < n/\ln n$, the gap $\sqrt{t \ln n} < \sqrt{n}$. The standard deviation of the sample quantile with $s=n$ is 0 (since it's the whole array). So if $t$ is very small, we can just use a different algorithm. The bound is asymptotic, so we can assume $t$ is large enough that $n^2/(t \ln n) \le n$, i.e., $t \ge n/\ln n$. For $t < n/\ln n$, we can use the trivial algorithm that takes $n + t \log n$? But $n + t \log n$ for $t = n/\ln n$ is $n + (n/\ln n) \log n = 2n$, which is $n + t + O(n)$, not $O(\sqrt{n \log n})$. Actually, for $t < n/\ln n$, the bound $n + t + O(\sqrt{n \log n})$ is $n + O(n/\ln n) + O(\sqrt{n \log n}) = n + O(\sqrt{n \log n})$. The tournament algorithm gives $n + t \log n = n + O(n)$? $t \log n = (n/\ln n) \log n = n$, so $2n$. That's larger than $n + O(\sqrt{n \log n})$. So we need a better algorithm for small $t$ as well.

Maybe the algorithm doesn't use a sample of fixed size. The hint says "with this many comparisons we can in fact find both...". It might be a non-constructive proof using the fact that the average number of comparisons to find the $t$th largest is at most the sum of the average number to find the $k_1$th and $k_2$th? Not sure.

Let's think about the average-case decision tree. The average depth of the decision tree for finding the $t$th largest is $\overline{V}_t(n)$. There might be a relation between $\overline{V}_t(n)$ and the entropy of the $t$th element. The bound $n + t + O(\sqrt{n \log n})$ might be derived from an information-theoretic argument? The entropy of the $t$th order statistic in a random permutation is $\log \binom{n}{t} + ...$? Not sure.

Wait, the exercise is marked HM40, which means it's a hard problem (HM = "mathematically difficult" or "homework"?). The hint is given. Perhaps the intended solution is to use the "median of medians" or "Floyd-Rivest" algorithm and analyze it to get the $O(\sqrt{n \log n})$ bound? But Floyd-Rivest gives $O(n^{2/3} \log^{1/3} n)$. Could there be a variant that gives $O(\sqrt{n \log n})$? If we use a sample of size $\sqrt{n \log n}$, the partition cost is $n$, the sample sorting cost is $\sqrt{n \log n} \log n = \sqrt{n} \log^{3/2} n$, which is $O(\sqrt{n \log n} \log n)$, not $O(\sqrt{n \log n})$. But we don't need to sort the sample; we only need to find two order statistics in the sample. Finding two order statistics in a sample of size $s$ takes $O(s)$ expected comparisons (using Quickselect). So the cost is $O(s) = O(\sqrt{n \log n})$. That works! The partition cost is $n + \min(k_1, n - k_2) \le n + t$. The recursion on the middle part: the middle part size is $O(\sqrt{t \ln n} + n/\sqrt{s})$. With $s = \sqrt{n \ln n}$, $n/\sqrt{s} = \sqrt{n \ln n} / \sqrt[4]{\ln n}$? Wait: $s = \sqrt{n \ln n}$. Then $n/\sqrt{s} = n / (n^{1/4} (\ln n)^{1/4}) = n^{3/4} / (\ln n)^{1/4}$. That's much larger than $\sqrt{n \ln n}$! So the deviation of the sample quantile is $O(n/\sqrt{s}) = O(n^{3/4} / (\ln n)^{1/4})$, which is huge compared to $\sqrt{n \ln n}$. So the middle part would be huge. We need $n/\sqrt{s} = O(\sqrt{t \ln n})$. So $s = \Omega(n^2/(t \ln n))$. For $t = n/2$, $s = \Omega(n/\ln n)$. Then $O(s) = O(n/\ln n)$, which is $o(n)$ but not $O(\sqrt{n \log n})$. The cost of finding the pivots in the sample would be $O(s) = O(n/\ln n)$, which is larger than $O(\sqrt{n \log n})$ for large $n$. So we need to balance.

Let's set $s = \max(\sqrt{n \ln n}, n^2/(t \ln n))$? For $t$ small, $s$ is large, making the cost $O(s)$ large. For $t$ large, $s$ is small. The total cost would be $n + t + O(\max(s, \text{middle part}))$. We need this to be $n + t + O(\sqrt{n \log n})$. So we need both $s = O(\sqrt{n \log n})$ and middle part $= O(\sqrt{n \log n})$. Middle part size is $O(\sqrt{t \ln n} + n/\sqrt{s})$. We want $n/\sqrt{s} = O(\sqrt{n \log n}) \implies \sqrt{s} = \Omega(\sqrt{n/\log n}) \implies s = \Omega(n/\log n)$. And we want $s = O(\sqrt{n \log n})$. So we need $n/\log n = O(\sqrt{n \log n}) \implies n = O(\sqrt{n} \log^{3/2} n) \implies \sqrt{n} = O(\log^{3/2} n)$, which is false for large $n$. So we cannot have both $s$ and $n/\sqrt{s}$ be $O(\sqrt{n \log n})$ simultaneously! The product $s \cdot (n/\sqrt{s}) = n \sqrt{s}$. To make both small, we need $s$ small and $n/\sqrt{s}$ small, which is impossible. The sum $s + n/\sqrt{s}$ is minimized when $s = n^{2/3}$, giving $n^{2/3}$. That's the Floyd-Rivest balance. So the error term cannot be $O(\sqrt{n \log n})$ with a single sample and single partition. It would be at least $\Omega(n^{2/3})$.

Wait, the Floyd-Rivest algorithm uses a two-level recursion: they sample, then partition, then recurse on a smaller set. The error term is $O(n^{2/3} \log^{1/3} n)$. To get $O(\sqrt{n \log n})$, we need a different approach. Maybe the algorithm doesn't use a sample. Maybe it uses a "tournament" with a different structure that exploits the random permutation to get $n + t + O(\sqrt{n \log n})$.

Let's reconsider the hint: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements". This is a statement about the total number of comparisons, not necessarily a single algorithm that finds them simultaneously. It might be a recursive argument: to find the $t$th element, we first find the $k_1$th and $k_2$th, and the total comparisons is bounded by a recurrence that solves to $n + t + O(\sqrt{n \log n})$.

Maybe we can find the $k_1$th and $k_2$th by a divide-and-conquer approach that splits the set into two halves, finds the relevant order statistics in each half, and combines them. The recurrence might be $\overline{V}t(n) \le \overline{V}{k_1}(n/2) + \overline{V}_{k_2}(n/2) + n/2 + ...$? Not sure.

Let's look at the section context again. The section is about "Minimum-Comparison Selection", which is worst-case. The exercises include both worst-case and average-case. Exercise 24 is about $\overline{V}_t(n)$, the average-case. The hint is from the author. Perhaps the algorithm is:

  • Use a binary search tree? No.

Another thought: The bound $n + t + O(\sqrt{n \log n})$ might be the expected number of comparisons for the "Quickselect" algorithm if we choose the pivot as the median of a sample of size $\sqrt{n \log n}$? Let's analyze Quickselect with sample size $s$. The expected number of comparisons is $n + O(n \log n / s)$? Actually, the expected number of comparisons for Quickselect with sample size $s$ is $n + O(n \log n / s) + O(s \log s)$? No, the analysis is more complex.

Let's derive the expected comparisons for a sample-based Quickselect. We want to find the $t$th largest. We take a sample of size $s$, find the $r$th largest in the sample where $r \approx s \cdot t/n$, use it as pivot, partition, and recurse. The expected number of comparisons satisfies: $C(n, t) = C_{\text{sample}}(s, r) + (n-1) + \mathbb{E}[C(n', t')]$. $C_{\text{sample}}(s, r)$ is the cost to find the $r$th in the sample, which is $O(s)$ if we use Quickselect recursively. The size of the recursive subproblem $n'$ is the distance of the pivot's true rank from $t$. The pivot's true rank has standard deviation $\sigma = O(n/\sqrt{s})$. The expected $n'$ is $O(\sigma) = O(n/\sqrt{s})$. So the expected cost satisfies $C(n) = n + O(s) + O(n/\sqrt{s})$ (assuming the recursion cost is linear in $n'$). To minimize $O(s + n/\sqrt{s})$, we set $s = n^{2/3}$, giving $O(n^{2/3})$. That's the Floyd-Rivest bound. If we want the total cost to be $n + t + O(\sqrt{n \log n})$, we need to incorporate the $t$ term. In the standard analysis, the expected size of the recursive subproblem is not $O(n/\sqrt{s})$ but actually $\min(t, n-t) + O(n/\sqrt{s})$? Wait, in Quickselect, the expected size of the subarray we recurse on is $\max(r, n-r)$ if we pick a random pivot? No, if we pick a pivot uniformly at random, the expected size of the larger side is $3n/4$. If we pick a pivot that is the median of a sample, the pivot's rank is concentrated around the median. The expected size of the subarray we recurse on is about $n/2 + O(n/\sqrt{s})$. But we are looking for the $t$th element. If the pivot's rank is $p$, and $t < p$, we recurse on the left side of size $p-1$. The expected size is $\mathbb{E}[p-1] \approx t + O(n/\sqrt{s})$. So the expected recursive size is $t + O(n/\sqrt{s})$. Then the total expected cost satisfies $C(n, t) = n + O(s) + C(t + O(n/\sqrt{s}), t')$. If we unroll this, the total cost is $n + t + O(s + n/\sqrt{s})$? Let's check: The first partition costs $n$. The recursive call has size $t + \delta$, where $\delta = O(n/\sqrt{s})$. The next partition costs $t + \delta$. The next recursive size is something like $t' + \delta'$, etc. Summing the partition costs: $n + t + \delta + \delta' + ...$. If the errors sum to $O(n/\sqrt{s})$, total cost is $n + t + O(n/\sqrt{s}) + O(s)$ (for sampling). To minimize $O(s + n/\sqrt{s})$, set $s = n^{2/3}$, giving $O(n^{2/3})$. So the $t$ term appears naturally as the sum of the sizes of the recursive subproblems? Actually, in Quickselect, the total cost is $n + n/2 + n/4 + ... = 2n$ for median. But if we use a pivot that is very close to the target, the recursive sizes decrease quickly. If the pivot is exactly the target, we stop after one partition, cost $n$. If the pivot is within $\delta$ of the target, the recursive size is $\delta$, cost $\delta$, etc. The total cost is $n + \delta + \delta_2 + ...$. If the initial pivot is the median of a sample of size $s$, $\delta = O(n/\sqrt{s})$. The next pivot is from a sample of the subarray? In Floyd-Rivest, they don't recurse with a new sample; they use the same sample? Actually, they use a sample at each level? Or they use a single sample and then recursively select in the subarray without sampling? The original Floyd-Rivest algorithm uses a single sample at the top level, then switches to a different algorithm (like Quickselect with no sample) for the subarray. That gives $n + t + O(n^{2/3} \log^{1/3} n)$? I need to recall exactly.

Let's check the paper: Floyd and Rivest, "Expected time bounds for selection", 1975. They present Algorithm SELECT which has expected comparisons $n + \min(k, n-k) + O(n^{2/3} \log^{1/3} n)$. The algorithm: If $n$ is small, sort. Otherwise, choose a sample of size $s = n^{2/3} \log^{1/3} n$. Find the $r$th and $(r+1)$th order statistics of the sample? Actually, they find two elements that bracket the target with high probability. They partition the whole array using these two pivots. Then they recursively apply SELECT on the middle part. The size of the middle part is $O(n/\sqrt{s}) = O(n^{2/3} \log^{1/3} n)$. The cost of the first partition is $n + O(\min(k, n-k))$? They use a clever partitioning that costs $n + \min(k, n-k) + O(1)$ expected comparisons? Actually, they partition by comparing each element to the two pivots in an order that minimizes expected comparisons. The expected number of comparisons is $n + \min(k, n-k) + O(1)$. Then they recurse on the middle part of size $O(n^{2/3} \log^{1/3} n)$. The recursion cost is $O(n^{2/3} \log^{1/3} n)$ because the middle part is small. So total cost = $n + \min(k, n-k) + O(n^{2/3} \log^{1/3} n)$.

Now, the exercise asks for $n + t + O(\sqrt{n \log n})$. This is a weaker bound for the first term (since $n^{2/3}$ is larger than $\sqrt{n}$) but a stronger bound for the second term (since $O(\sqrt{n \log n})$ is smaller than $O(n^{2/3})$). Wait, $n^{2/3}$ grows faster than $\sqrt{n}$ for large $n$. So $O(\sqrt{n \log n})$ is a smaller error term. But Floyd-Rivest has $O(n^{2/3} \log^{1/3} n)$ error term, which is larger. So Floyd-Rivest does not meet the $O(\sqrt{n \log n})$ bound. However, there might be a simpler algorithm that achieves $O(\sqrt{n \log n})$ by using a different technique, perhaps by using a single pivot and a different sampling strategy? But we saw that the trade-off gives $n^{2/3}$. How can we get $\sqrt{n}$? Maybe we can use a sample of size $s = \sqrt{n \log n}$ and then do something else that reduces the error.

Let's analyze the Floyd-Rivest algorithm with sample size $s = \sqrt{n \ln n}$. The middle part size is $O(n/\sqrt{s}) = O(n^{3/4} / (\ln n)^{1/4})$, which is $\omega(\sqrt{n \log n})$. Then they recurse on the middle part. If they recurse on the middle part using the same algorithm, the total cost would be $n + t + \text{size of middle part} + ...$. The middle part size is $n^{3/4}$, which is larger than $\sqrt{n}$. So that doesn't give $\sqrt{n}$.

Maybe the algorithm in the hint is not Floyd-Rivest but a different one that uses a tournament to find the $k_1$th and $k_2$th with $n + t$ comparisons on average? Is there a tournament algorithm that on average uses $n + t$ comparisons? Consider the following: To find the $t$th largest, we can do a "partial tournament" where we only keep track of the top $t$ elements. We can build a tournament tree of size $t$? No, a tournament tree finds the maximum. To find the $t$th largest, we could run a knockout tournament among all elements, but we only need to determine the top $t$ elements. The standard tournament to find the maximum takes $n-1$ comparisons. To find the second maximum, we need $\lceil \log n \rceil - 1$ additional comparisons. To find the third, we need more. The total comparisons to find the first $t$ elements in a tournament is $n + t \log n - \text{something}$. But that's worst-case. The average-case over random permutations: In a tournament tree, the comparisons are fixed by the tree structure. The outcomes are random. The number of comparisons to find the $t$th largest is deterministic? Actually, the tournament algorithm to find the $t$th largest is: build the tree, then iteratively find the next largest by looking at the opponents of the previously found elements. The set of comparisons is deterministic given the outcomes? The algorithm might compare specific pairs based on the tree. The total number of comparisons is fixed regardless of the permutation? For finding the maximum, it's always $n-1$. For finding the second maximum, we always compare the $\lceil \log n \rceil$ opponents of the maximum among themselves, which takes $\lceil \log n \rceil - 1$ comparisons. So total is fixed. The average-case is the same as worst-case. So tournament doesn't give $n + t$ for large $t$.

What about an algorithm that uses a binary search tree (BST)? Insert elements into a BST. The expected number of comparisons to insert $n$ elements is $O(n \log n)$. Not good.

Maybe the algorithm is: "Find the $t$th largest by doing a linear scan and keeping a set of $t$ candidates, but using a data structure that allows insertion in $O(1)$ amortized comparisons?" There is a data structure called a "tournament tree" or "heap" that doesn't achieve $O(1)$ insertion.

Wait, there is a known result: The average number of comparisons to find the $t$th largest is $n + t + O(\sqrt{n \log n})$ for $t \le n/2$. This is a result by "K. J. Supowit" or "F. K. Hwang"? I'm not sure.

Let's think about the hint again: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements". This is a very specific hint. It might be from a paper by "L. Hyafil" or "P. V. Ramanan"? The preceding exercises 21-23 mention Ramanan, Hyafil, Kirkpatrick. Exercise 21 is by Ramanan and Hyafil. Exercise 22 by Kirkpatrick. Exercise 23 asks for asymptotic value of $V_{\lceil n/2 \rceil}(n)$. Exercise 24 is HM40. The hint might be from the solution by the author.

Maybe the algorithm is: Use a "median of medians" type approach but with a different group size? No, that's worst-case.

Another idea: The bound $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$ might be proven by a non-constructive argument using the fact that the optimal decision tree has average depth at most something. The hint might be a way to construct a decision tree: we first find the $k_1$th and $k_2$th elements, which are easy to find because they are far from $t$? But $k_1$ and $k_2$ are close to $t$.

Let's try to find the source of this exercise. It's from TAOCP Vol 3, Section 5.3.3, Exercise 24. The hint is in the problem statement. The solution might be in the book's answers? I don't have the answers, but I can reason.

Perhaps the algorithm is:

  1. Randomly partition the $n$ elements into two halves of size $n/2$ (or roughly equal).
  2. Find the $k_1$th and $k_2$th largest in each half? Not sure.

Wait, the hint says "find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements". This suggests we need to find two specific order statistics. Maybe we can find them by running the selection algorithm twice? If $\overline{V}t(n) \le n + t + O(\sqrt{n \log n})$, then $\overline{V}{k_1}(n) \le n + k_1 + O(\sqrt{n \log n})$ and $\overline{V}_{k_2}(n) \le n + k_2 + O(\sqrt{n \log n})$. If we run them independently, the total would be $2n + t + ...$, too large. But we can find them simultaneously with a single pass? How?

Consider the following algorithm: We want to find the $t$th largest. We can do a binary search on the value? Not without a bounded universe.

Maybe the algorithm is:

  • Use a sample of size $s = \sqrt{n \ln n}$.
  • Sort the sample.
  • The sample divides the elements into $s+1$ buckets. We can find the exact rank of each sample element by comparing all elements to the sample? That would be $n \log s$ comparisons if we use binary search. That's $n \log n$, too much.

What if we don't sort the sample? We just find the $r_1$th and $r_2$th in the sample using Quickselect. Then we partition the whole array with respect to these two pivots. The expected number of comparisons for partitioning is $n + \min(k_1, n - k_2) \le n + t$. The cost to find the pivots in the sample is $O(s)$. The size of the middle part is $O(\sqrt{t \ln n} + n/\sqrt{s})$. If we choose $s = n^2/(t \ln n)$, then $n/\sqrt{s} = \sqrt{t \ln n}$. But $s$ might be larger than $n$. If $s > n$, we just use the whole array as the sample? If $s \ge n$, then we are just selecting directly, which costs $O(n)$? Actually, if $t$ is small, $s$ required to make $n/\sqrt{s} = O(\sqrt{t \ln n})$ is $s = \Omega(n^2/(t \ln n))$. For $t = O(1)$, $s = \Omega(n^2/\ln n) \gg n$. So we cannot use a sample of size $s$ to achieve that deviation. For small $t$, we must use a different method. But the bound $n + t + O(\sqrt{n \log n})$ for small $t$ is $n + O(\sqrt{n \log n})$. There are algorithms that find the $t$th largest for small $t$ in $n + O(\sqrt{n \log n})$ expected comparisons? For example, the algorithm by "Hoare" (Quickselect) for $t=1$ is $n-1$, which is $n + O(1)$. For $t=2$, Quickselect averages $2n$? No, Quickselect for $t=2$ might average $n + \log n$? Actually, the standard Quickselect for the second largest: pick a pivot, partition. If the pivot is the maximum, we recurse on the left side of size $n-1$ to find the maximum there. The expected number of comparisons is $n-1 + \frac{1}{n} \sum_{i=1}^n \text{cost}(i-1, \text{new target})$. This might sum to $2n$? I'm not sure. There are algorithms that find the $k$th largest for small $k$ in $n + k \log n$ worst-case, and average-case might be $n + k + o(n)$? The bound $n + t + O(\sqrt{n \log n})$ is an upper bound, so it's true if there exists some algorithm achieving it. The problem asks to prove the bound, so we just need to exhibit an algorithm and analyze it.

Maybe the algorithm is the "Floyd-Rivest" algorithm, and the $O(\sqrt{n \log n})$ is a typo or I'm misremembering the error term. Let's check the error term in Floyd-Rivest: They give $n + \min(k, n-k) + O(n^{2/3} \log^{1/3} n)$. That's the best known? There is a later improvement by "Karloff and Raghavan" (1990) "Randomized algorithms and expected time bounds for selection" which gives $n + \min(k, n-k) + O(\sqrt{n \log n})$? I think Karloff and Raghavan improved the error term to $O(\sqrt{n \log n})$! Yes, I recall a paper: "On the average number of comparisons for selection" or something. Karloff and Raghavan (1990) gave an algorithm with expected comparisons $n + \min(k, n-k) + O(\sqrt{n \log n})$. The hint with $\sqrt{t \ln n}$ matches their analysis. The algorithm is: take a random sample of size $s = \sqrt{n \ln n}$? Wait, Karloff and Raghavan use a sample of size $\sqrt{n \log n}$ and a different partitioning strategy? Let's recall: The Karloff-Raghavan algorithm uses a sample of size $\sqrt{n \log n}$ and then does a three-way partition. But as we saw, the deviation is $n/\sqrt{s} = n / n^{1/4} = n^{3/4}$, which is larger than $\sqrt{n \log n}$. How do they achieve $\sqrt{n \log n}$? They might use a two-level sampling: first a large sample to get a rough pivot, then a smaller sample? Or they use a recursive sampling strategy? Actually, the paper "Randomized algorithms and expected time bounds for selection" by Karloff and Raghavan (1990) presents an algorithm with expected comparisons $n + \min(k, n-k) + O(\sqrt{n \log n})$. The algorithm is:

  1. Choose a random sample of size $s = \sqrt{n \log n}$.
  2. Sort the sample.
  3. Use the sample to find two pivots that bracket the target rank with high probability.
  4. Partition the array with respect to these two pivots.
  5. Recurse on the middle part.

But we saw the middle part size is $O(n/\sqrt{s}) = O(n^{3/4} / (\log n)^{1/4})$, which is not $\sqrt{n \log n}$. How do they get $\sqrt{n \log n}$? Maybe they use a different sample size at each recursion level, like a geometric decrease? Or they use a sample of size $s$ and then only need to sort the middle part, which is of size $O(\sqrt{n \log n})$ if $s = n / \sqrt{n \log n}$? That would be $s = \sqrt{n / \log n}$, then $s \log s = \sqrt{n / \log n} \log n = \sqrt{n \log n}$. And the deviation is $n/\sqrt{s} = n / (n^{1/4} / (\log n)^{1/4}) = n^{3/4} (\log n)^{1/4}$, which is larger. So the middle part is large. They might use a two-stage sampling: first a large sample to reduce the problem size to $n^{3/4}$, then a smaller sample? That would give a larger error.

Let's search my memory for the exact algorithm. I think the Karloff-Raghavan algorithm uses a sample of size $s = \sqrt{n \log n}$ and then does a three-way partition, but they don't recurse on the middle part by sorting it; they use a different method to find the exact element within the middle part that takes $O(\sqrt{n \log n})$ comparisons. But the middle part size is $O(n/\sqrt{s}) = O(n^{3/4})$, which is larger than $\sqrt{n \log n}$. So they would need to do more than $O(\text{middle part size})$ comparisons. That seems impossible.

Maybe I have the error term wrong. Let's check: Floyd-Rivest (1975): $n + \min(k, n-k) + O(n^{2/3} \log^{1/3} n)$. Karloff-Raghavan (1990): $n + \min(k, n-k) + O(\sqrt{n \log n})$? I'm not sure. There is a paper by "Karp and Raghavan" (1991) "Randomized algorithms and expected time bounds for selection" maybe? Actually, "Karloff and Raghavan" is a different paper. I recall a result by "K. R. Mount" or "R. W. Floyd"? Wait, the bound $n + t + O(\sqrt{n \log n})$ appears in an exercise in Knuth's book. The hint is from Knuth. The algorithm might be a simpler one that Knuth devised for the exercise.

Let's read the hint again: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." The phrase "with this many comparisons" refers to $n + t + O(\sqrt{n \log n})$. So the algorithm's total comparisons is $n + t + O(\sqrt{n \log n})$. It finds both $k_1$th and $k_2$th. Then the $t$th is easily located among the elements between them. The number of elements between $k_1$th and $k_2$th is $k_2 - k_1 + 1 = O(\sqrt{t \ln n})$. If we can find the $t$th among these $O(\sqrt{t \ln n})$ elements in $O(\sqrt{t \ln n})$ comparisons, that's $O(\sqrt{n \log n})$ additional. So the algorithm to find $k_1$th and $k_2$th must take $n + t + O(\sqrt{n \log n})$.

How to find both $k_1$th and $k_2$th with $n + t + O(\sqrt{n \log n})$? Perhaps we can find them by a single pass that maintains the top $k_2$ elements? But we saw that costs $n + O(k_2 \log k_2)$, which is $n + t \log t$ for large $t$. That's too large.

What if we use a "binary insertion" but with a twist? No.

Maybe the algorithm is:

  1. Find the $k_1$th largest using an algorithm that takes $n + k_1 + O(\sqrt{n \log n})$ comparisons.
  2. Then, among the elements smaller than the $k_1$th largest, find the $(k_2 - k_1)$th largest. The number of such elements is $n - k_1$. The target rank is $k_2 - k_1 = O(\sqrt{t \ln n})$. If we can find the $m$th largest among $M$ elements in $M + m + O(\sqrt{M \log M})$ comparisons, then the second step costs $(n - k_1) + (k_2 - k_1) + O(\sqrt{n \log n}) = n - 2k_1 + k_2 + O(\sqrt{n \log n})$. Summing with first step: $(n + k_1) + (n - 2k_1 + k_2) = 2n - k_1 + k_2$. That's about $2n + 2\sqrt{t \ln n}$, not $n + t$.

But what if we find the $k_2$th largest first? Same issue.

What if we find them simultaneously by a single partition with two pivots? If we choose pivots $p$ and $q$ that are close to $k_1$th and $k_2$th, we can partition the array in one pass. The expected comparisons for the partition is $n + \min(|L|, |R|) \approx n + \min(k_1, n - k_2) \le n + t$. Then the $k_1$th and $k_2$th are in the middle part of size $O(\sqrt{t \ln n} + \text{deviation})$. We need to find them exactly within the middle part. If we can find them within the middle part in $O(\sqrt{t \ln n})$ comparisons, total is $n + t + O(\sqrt{n \log n})$. The challenge is to get the pivots $p$ and $q$ such that the middle part is small, and to find the pivots with $O(\sqrt{n \log n})$ comparisons.

How to get such pivots? We can use a random sample. But we need the sample size $s$ such that the deviation $n/\sqrt{s}$ is $O(\sqrt{t \ln n})$. This requires $s = \Omega(n^2/(t \ln n))$. If $t$ is large (e.g., $t = n/2$), $s = \Omega(n/\ln n)$. The cost to find the pivots in the sample is $O(s) = O(n/\ln n)$, which is $o(n)$ but not $O(\sqrt{n \log n})$ (since $n/\ln n \gg \sqrt{n \log n}$ for large $n$). So the cost of finding the pivots would be too large for the $O(\sqrt{n \log n})$ bound. Unless we don't need to find them exactly in the sample? Maybe we can just pick the pivots randomly from the array? If we pick two random elements as pivots, the expected partition cost is $n + \min(\text{rank}_1, n - \text{rank}_2)$. If we want $\min(\text{rank}_1, n - \text{rank}_2) \le t$, we need the pivots to be within the top $t$ or bottom $n-t$? That's unlikely if $t$ is small.

Wait, maybe the algorithm doesn't use a sample at all. It uses a deterministic method that on average performs well. For example, the "median of medians" is deterministic and linear, but that's worst-case. The average-case of the median-of-medians algorithm might be $n + t + O(\sqrt{n \log n})$? I doubt it.

Let's think about the possibility that $\overline{V}_t(n)$ is defined as the minimum average number of comparisons over all deterministic algorithms, assuming the input is a random permutation. The bound might be proven by analyzing the optimal decision tree. The hint might be a way to construct a decision tree: we can first find the $k_1$th and $k_2$th elements by a tree that has average depth $n + t + O(\sqrt{n \log n})$. This could be a recursive construction: to find the $t$th element, we compare it to some elements, and then we need to find two other order statistics in smaller sets.

Maybe the algorithm is:

  • Choose a pivot element $x$.
  • Partition the array into elements less than $x$ and greater than $x$. Let the rank of $x$ be $r$.
  • If $r = t$, we are done.
  • If $r > t$, the $t$th element is in the set of elements greater than $x$, which has size $r-1$. We need to find the $t$th largest among them.
  • If $r < t$, the $t$th element is in the set of elements less than $x$, size $n-r$. We need to find the $(t-r)$th largest among them. This is Quickselect. The average number of comparisons is $2n$. To reduce it to $n + t$, we need to choose $x$ such that the expected size of the recursive call is small. If we choose $x$ to be the median of a random sample of size $s$, the expected size of the recursive call is $t + O(n/\sqrt{s})$ if $r > t$, or $n-t + O(n/\sqrt{s})$ if $r < t$. The total expected comparisons is $n + t + O(n/\sqrt{s}) + O(s \log s)$? Wait, we don't need to sort the sample; we can find the median of the sample in $O(s)$ expected comparisons using Quickselect. So the cost is $n + t + O(s + n/\sqrt{s})$. To get $O(\sqrt{n \log n})$, we set $s = n^{2/3}$, giving $O(n^{2/3})$. That's not $O(\sqrt{n \log n})$.

What if we use a different pivot selection? Instead of taking a sample, we could use a deterministic pivot like the median of the first few elements? The average-case might still be $2n$.

Maybe the algorithm is not recursive. The hint says "find both the $k_1$th and $k_2$th elements". This suggests we find them directly, not by recursively finding $t$. How can we find $k_1$th and $k_2$th directly with $n + t$ comparisons? If we can find the $t$th element with $n + t$ comparisons, then we can also find $k_1$th and $k_2$th by running the same algorithm? But running it three times would triple the comparisons.

Wait, maybe the algorithm finds $k_1$th and $k_2$th as a byproduct of finding the $t$th element? Or maybe the algorithm to find the $t$th element naturally identifies a range of elements that includes $k_1$th and $k_2$th?

Let's consider the following algorithm (similar to Quickselect but with two pivots):

  1. Choose two pivots $p$ and $q$ from the array (maybe the first two elements?).
  2. Partition the array into three parts: $L = {x > p}$, $M = {p \ge x \ge q}$, $R = {x < q}$. Assume $p > q$.
  3. Let $l = |L|$, $m = |M|$, $r = |R|$.
  4. If $k_1 \le l$, then the $k_1$th element is in $L$, and we recurse on $L$ to find it. Similarly for $k_2$. But we want to find both $k_1$ and $k_2$. If we choose $p$ and $q$ such that $l \approx k_1$ and $r \approx n - k_2$, then $m \approx k_2 - k_1 = O(\sqrt{t \ln n})$. Then $k_1$th is either in $M$ or is $p$? Actually, if $l \approx k_1$, then the $k_1$th element is the smallest in $L$ or the largest in $M$? Wait, if $l = k_1 - 1$, then the $k_1$th element is the largest in $M$. If $l = k_1$, then the $k_1$th is the smallest in $L$. So if $l$ is close to $k_1$, the $k_1$th is near the boundary between $L$ and $M$. Similarly for $k_2$. So if we choose $p$ and $q$ such that $l$ is close to $k_1$ and $r$ is close to $n - k_2$, then the $k_1$th and $k_2$th are in the small middle part $M$ (or are $p$ and $q$). Then we can find them exactly by sorting $M$ or selecting within $M$. The cost of partitioning is $n + \min(l, r) \approx n + \min(k_1, n - k_2) \le n + t$. The cost of selecting within $M$ is $O(|M|) = O(\sqrt{t \ln n} + \text{deviation})$. The deviation depends on how close $l$ and $r$ are to the targets. If we choose $p$ and $q$ as random elements, the deviation is large. If we choose them from a sample, we need the sample size to control the deviation.

The deviation of the rank of a random sample quantile is $O(n/\sqrt{s})$. To make the deviation $O(\sqrt{t \ln n})$, we need $s = \Omega(n^2/(t \ln n))$. For $t$ small, this $s$ is large. The cost to find the pivots in the sample is $O(s)$ (using Quickselect on the sample). So the total cost is $n + t + O(s + n/\sqrt{s})$. To minimize this, we set $s = n^{2/3}$ giving $O(n^{2/3})$. To get $O(\sqrt{n \log n})$, we need to set $s$ differently? The sum $s + n/\sqrt{s}$ is minimized at $n^{2/3}$. Its minimum value is $\Theta(n^{2/3})$. So any algorithm that uses a single sample and a single partition will have error $\Omega(n^{2/3})$. To achieve $O(\sqrt{n \log n})$, we must use a different strategy that doesn't have this trade-off.

What strategy can achieve $\sqrt{n}$? Perhaps the algorithm uses a "median of medians" approach but with a different group size? That gives worst-case linear, not average-case with $\sqrt{n}$ error.

Maybe the algorithm is: "Use a tournament tree to find the maximum, then the second maximum, etc., but stop after $t$ steps, and use the fact that on average the tournament tree doesn't need to compare all opponents?" But we saw the tournament tree's comparisons are fixed. Unless we use a different tournament structure, like a "knockout tournament" where losers are not immediately eliminated? Lewis Carroll's tournament? That's mentioned in the text. The text says Lewis Carroll's tournament is not optimal but has some interesting aspects. Maybe the exercise is about analyzing Carroll's tournament? The text says: "It would be nice to report that Lewis Carroll's tournament turns out to be optimal, but unfortunately that is not the case. His procedure makes more comparisons than necessary... And it appears to be an excellent plan for a tennis tournament, because he built in some dramatic effects..." But the exercises are about minimum-comparison selection. Exercise 24 might be about an average-case analysis of a different algorithm.

Let's read the section context again: "We shall prove Kislitsyn's theorem by showing that the first $t$ stages of tree selection require at most $n - t + \sum_{n+1-t < j \le n} \lceil \lg j \rceil$ comparisons..." This is worst-case. Exercise 21 improves this bound for specific $n$. Exercise 22 improves $V_3(n)$ for specific $n$. Exercise 23 asks for asymptotic value of $V_{\lceil n/2 \rceil}(n)$. Exercise 24 is about $\overline{V}_t(n)$. The hint: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located."

Maybe the algorithm is:

  • Use a "binary search" on the values by comparing elements to a randomly chosen pivot? No.

Another thought: The bound $n + t + O(\sqrt{n \log n})$ might be the expected number of comparisons for the "Floyd-Rivest" algorithm if we use a sample of size $\sqrt{n \log n}$ and then use a different method to find the exact element within the middle part that takes $O(\sqrt{n \log n})$ comparisons, but the middle part size is larger? That can't be.

Wait, maybe the sample size is chosen to be $s = \sqrt{n \log n}$, and the middle part size is $O(\sqrt{n \log n})$ because the deviation of the sample quantile is not $n/\sqrt{s}$ but something else? If we take a random sample of size $s$, the rank of the $r$th sample element is a hypergeometric random variable. Its standard deviation is $\sqrt{ \frac{r(s-r)}{s+1} \cdot \frac{n-s}{n-1} } \approx n \sqrt{ \frac{r(s-r)}{s^3} }$. For $r = \alpha s$, standard deviation $\approx n \sqrt{\alpha(1-\alpha)/s}$. So it's $n/\sqrt{s}$. That's correct.

Is there a way to get the pivots without a sample? Maybe we can use the "median of medians" algorithm to find the $k_1$th and $k_2$th exactly in linear time, and the average-case is $n + t + O(\sqrt{n \log n})$? The median-of-medians algorithm has worst-case linear comparisons, but the constant factor is high (like 5n or something). The average-case of that algorithm is also linear with the same constant? Not $n + t$.

Wait, maybe the algorithm is the "Quickselect" algorithm but with a different analysis that shows the expected number of comparisons is $n + t + O(\sqrt{n \log n})$? Let's check the expected number of comparisons for Quickselect to find the $t$th largest. The standard Quickselect picks a random pivot, partitions, and recurses. The expected number of comparisons $C(n,t)$ satisfies $C(n,t) = n-1 + \frac{1}{n} \sum_{i=1}^n C(\text{subproblem size})$. For $t \le n/2$, the expected size of the subproblem is about $t + n/2$? No, if the pivot rank is $i$, and $i > t$, we recurse on left side of size $i-1$. The expected size is $\frac{1}{n} \sum_{i=t+1}^n (i-1) \approx \frac{n^2 - t^2}{2n} = \frac{n}{2} - \frac{t^2}{2n}$. For $t$ small, this is $n/2$, not $t$. So the recursion size is $n/2$, leading to $2n$ total. So Quickselect doesn't give $n + t$.

What if we choose the pivot as the median of a sample of size $s$? Then the pivot's rank is concentrated around $n/2$. For $t$ small, the pivot is likely larger than $t$, so we recurse on the left side. The size of the left side is the pivot's rank minus 1, which is around $n/2$. Still $n/2$. So that doesn't help for small $t$.

To get $n + t$, we need the pivot's rank to be close to $t$. So we need to choose the pivot from the sample at the appropriate quantile $t/n$. That's what Floyd-Rivest does. The expected size of the recursive call is $t + O(n/\sqrt{s})$. So the total cost is $n + t + O(s + n/\sqrt{s})$. Minimizing gives $s = n^{2/3}$, error $n^{2/3}$. To get $n + t + O(\sqrt{n \log n})$, we need $s + n/\sqrt{s} = O(\sqrt{n \log n})$. This is impossible because the minimum of $s + n/\sqrt{s}$ is $\Omega(n^{2/3})$. Therefore, any algorithm that uses a single sample and a single partition cannot achieve $O(\sqrt{n \log n})$ error. It must use a different approach, perhaps using multiple levels of sampling or a different structure altogether.

Maybe the algorithm uses a "tournament" that is not a fixed tree but adapts based on outcomes? There is a known algorithm by "Schönheim" or "Kislitsyn" for worst-case. For average-case, maybe there's an algorithm that does the following:

  • Compare the elements in pairs, winners advance, etc. But keep track of the losers in a way that on average you don't need many comparisons.

Wait, the hint says "find both the $k_1$th and $k_2$th elements". Could it be that we find them by doing a single pass that computes the ranks of all elements? That would be $O(n^2)$. No.

Let's think about the phrase "with this many comparisons we can in fact find both...". This might be a statement about the existence of a decision tree with average depth $n + t + O(\sqrt{n \log n})$. The proof might be by induction on $n$. We can use a divide-and-conquer approach: split the array into two halves of size $n/2$, find the appropriate order statistics in each half, and combine them. The recurrence might be $\overline{V}t(n) \le \overline{V}{a}(n/2) + \overline{V}_{b}(n/2) + n/2 + O(\sqrt{n \log n})$. If we can choose $a$ and $b$ such that $a+b = t$ or something, we might get the bound.

Actually, there is a known result by "D. G. Kirkpatrick" (Exercise 22) who gives a method to find the third largest with fewer comparisons. Maybe Exercise 24 is by Kirkpatrick or another author.

Let's search my memory for "n + t + O(sqrt(n log n))" in the context of average-case selection. I recall a paper by "K. J. Supowit" "A general lower bound for the number of comparisons needed to find the t-th largest element" or something. But that's lower bound.

Maybe the algorithm is:

  1. Randomly order the elements.
  2. For i = 1 to n, insert element i into a sorted list of the largest elements seen so far, but stop when the list size reaches t+something? Not sure.

Another idea: The bound $n + t + O(\sqrt{n \log n})$ is exactly the expected number of comparisons for the algorithm that finds the $t$th largest by maintaining a set of $t$ candidates in a tournament tree? There is an algorithm by "I. Wegener" or "R. W. Floyd"?

Let's try to derive the bound from first principles using the hint. The hint says: Show that we can find both $k_1$ and $k_2$ with $n + t + O(\sqrt{n \log n})$ comparisons. Then the $t$th is easily located. So the total is $n + t + O(\sqrt{n \log n})$.

Suppose we have an algorithm that finds the $k$th largest element with $n + k + O(\sqrt{n \log n})$ expected comparisons. Then to find $k_1$ and $k_2$, we could run it twice? That would be $2n + t + ...$, too much. But maybe we can find $k_1$ and $k_2$ simultaneously with the same algorithm? If we run the algorithm to find $k_1$, it partitions the array and leaves the elements in some state. We might be able to reuse the information to find $k_2$ with only $k_2 - k_1$ additional comparisons. If the first algorithm costs $n + k_1 + O(\sqrt{n \log n})$, and the second costs $k_2 - k_1 + O(\sqrt{n \log n})$, total is $n + k_2 + O(\sqrt{n \log n})$. Since $k_2 \le t + \sqrt{t \ln n}$, this is $n + t + O(\sqrt{n \log n})$. That would work! So we need an algorithm that, after finding the $k_1$th largest, can find the $k_2$th largest (which is smaller) with only $k_2 - k_1$ additional comparisons on average. How? If the algorithm for $k_1$ partitions the array and leaves the elements smaller than the $k_1$th largest in a data structure that allows selecting the $m$th largest among them in $m$ comparisons? For example, if the algorithm builds a tournament tree that naturally organizes the elements so that the next largest can be found in $O(1)$ comparisons? The standard tournament tree for finding the maximum: after finding the maximum, the second maximum is among the opponents of the maximum. We can find it in $\log n$ comparisons. The third maximum can be found by looking at the opponents of the second maximum, etc. The total comparisons to find the first $t$ elements is $n + t \log n$ worst-case. But on average, if the tournament tree is built on a random permutation, the number of comparisons to find the $i$th element given the first $i-1$ might be $O(1)$? Let's check: In a knockout tournament, the champion has played $\log n$ matches. The opponents are the $\log n$ players who lost directly to the champion. The second place is the maximum among them, taking $\log n - 1$ comparisons. The third place is the maximum among the opponents of the champion and the second place? Actually, the third place is the maximum among the opponents of the champion and the second place, excluding the second place itself. The number of such opponents is at most $\log n$. Finding the third takes $\log n$ comparisons. In general, finding the $i$th place takes at most $\log n$ comparisons, because we only need to consider the opponents of the already found top $i-1$ players. The total comparisons is $n + t \log n$. That's not $n + t$.

But what if the tournament is not a balanced tree? If we use a different tournament structure, like a "selection tree" that is not balanced? The text mentions "tree selection" in Section 5.2.3. The worst-case for tree selection is given by Kislitsyn's formula. The average-case of tree selection might be better? If the input is a random permutation, the comparisons in the tree might be less? Actually, the tree selection algorithm's comparisons are deterministic given the outcomes. The number of comparisons to find the $t$th largest using the optimal tree selection (as per Kislitsyn) is $n - t + \sum \lceil \log j \rceil$. This is $n + t \log n$ for small $t$. The average-case is the same because the algorithm is deterministic and the number of comparisons doesn't depend on the values, only on the outcomes? In a knockout tournament, the comparisons are predetermined by the tree structure. The number of comparisons is fixed regardless of the values. So the average-case is the same as worst-case. So tree selection doesn't give $n + t$.

Maybe the algorithm uses a "binary search tree" built by inserting elements in random order? The expected depth of the $t$th largest element in a random BST is $O(\log n)$. Not $n + t$.

Wait, maybe the algorithm is: "Find the $k_1$th and $k_2$th by using a single pivot and then a linear scan?" Let's think differently. The hint might be suggesting a specific algorithm that is known as "Quickselect with a good pivot" but with a specific choice of pivot that makes the expected number of comparisons exactly $n + t + O(\sqrt{n \log n})$. How could the expected number be $n + t$? If we pick a pivot that is exactly the $t$th element with probability $p$, and otherwise we get a subproblem of size $O(\sqrt{n \log n})$, then the expected cost is $n + O(\sqrt{n \log n})$. But we need $n + t$. For $t$ large, $n + t$ is larger than $n$. So the algorithm must inherently do $t$ comparisons in addition to $n$.

What if the algorithm is: "Scan the elements, and for each element, compare it with the current $t$th largest candidate. If it's larger, update the candidate set." If we maintain the top $t$ elements in a sorted list, the expected number of insertions is $t \ln(n/t)$, and each insertion takes $\log t$ comparisons. That's $n + t \ln(n/t) \log t$. For $t = n/2$, this is $n + (n/2) \ln 2 \log(n/2) = n + \Omega(n \log n)$, which is much larger than $n + t + O(\sqrt{n \log n})$. So that's not it.

What if we maintain the top $t$ elements in a tournament tree? Inserting an element into a tournament tree of size $t$ takes $O(\log t)$ comparisons. The expected number of insertions is $t \ln(n/t)$. Total $n + t \ln(n/t) \log t$. Same issue.

What if we use a "selection algorithm" that finds the $t$th largest by doing a binary search on the value? If the elements are numbers, we can't do that without knowing the range.

Maybe the elements are arbitrary and we only have comparisons. The average-case bound $n + t + O(\sqrt{n \log n})$ is a known result by "Karloff and Raghavan" (1990). Let me try to recall their algorithm. Karloff and Raghavan, "Randomized algorithms and expected time bounds for selection", Algorithmica 1990. They give an algorithm with expected comparisons $n + \min(k, n-k) + O(\sqrt{n \log n})$. The algorithm is:

  1. Take a random sample of size $s = \sqrt{n \log n}$.
  2. Find the $r_1$th and $r_2$th order statistics of the sample, where $r_1 = \lfloor s \cdot (k - \sqrt{k \log n})/n \rfloor$ and $r_2 = \lceil s \cdot (k + \sqrt{k \log n})/n \rceil$.
  3. Use these as pivots to partition the array.
  4. The middle part will have size $O(\sqrt{k \log n})$ with high probability.
  5. Recurse on the middle part? But as we saw, the middle part size is $O(n/\sqrt{s}) = O(n^{3/4} / (\log n)^{1/4})$, which is not $O(\sqrt{k \log n})$. How do they get the middle part size to be $O(\sqrt{k \log n})$? They might use a larger sample? If they set $s = n^2/(k \log n)$, then for $k$ small, $s > n$. For $k$ small, they might use a different strategy. The paper might have a two-phase algorithm: for $k < n/\log n$, use a different method (like a heap) that gives $n + O(\sqrt{n \log n})$? Actually, for $k$ small, $n + k + O(\sqrt{n \log n})$ is $n + O(\sqrt{n \log n})$. There are algorithms that find the $k$th largest for small $k$ in $n + O(\sqrt{n \log n})$ expected comparisons? I'm not sure.

Let's look at the hint again: $\lfloor t - \sqrt{t \ln n} \rfloor$ and $\lceil t + \sqrt{t \ln n} \rceil$. The gap is $2\sqrt{t \ln n}$. This is exactly the gap used in the Karloff-Raghavan paper. They use this gap to ensure that the middle part is small. But the sample size they use is $s = \sqrt{n \log n}$? Let's check: In Karloff-Raghavan, they use a sample of size $s = \sqrt{n \log n}$. The deviation of the sample quantile is $O(n/\sqrt{s}) = O(n^{3/4} / (\log n)^{1/4})$. They don't use the sample quantile directly as the pivot; they use it to partition the array, and then they recurse on the middle part? But the middle part would be of size $O(n^{3/4})$, which is larger than $\sqrt{n \log n}$. They might then use a different algorithm on the middle part that takes $O(\text{middle part size})$ comparisons, leading to a total error of $O(n^{3/4})$, not $\sqrt{n \log n}$. So that's not it.

Wait, maybe the sample size is not $\sqrt{n \log n}$ but something else. The hint says "with this many comparisons we can in fact find both...". It might be a recursive argument where the algorithm is not sample-based but uses a divide-and-conquer that splits the problem into two halves of size $n/2$?

Consider the following: To find the $t$th largest, we can find the $t_1$th largest in the first half and the $t_2$th largest in the second half, and combine them. The recurrence might be $\overline{V}t(n) \le \overline{V}{t_1}(n/2) + \overline{V}_{t_2}(n/2) + n/2 + O(\sqrt{n \log n})$. If we can choose $t_1$ and $t_2$ appropriately, we might get the bound. This is similar to the "median of medians" but for average-case.

Actually, there is a known algorithm by "Hoare" (Quickselect) that has average $2n$. There is an algorithm by "Floyd and Rivest" that has $n + \min(k, n-k) + O(n^{2/3} \log^{1/3} n)$. There is an algorithm by "Karloff and Raghavan" that has $n + \min(k, n-k) + O(\sqrt{n \log n})$. The Karloff-Raghavan algorithm uses a sample of size $s = \sqrt{n \log n}$ and a clever recursive partitioning that reduces the error to $\sqrt{n \log n}$. Let me try to reconstruct it.

Karloff-Raghavan algorithm (from memory):

  • If $n$ is small, sort.
  • Choose a random sample of size $s = \sqrt{n \log n}$.
  • Sort the sample.
  • Let $p$ be the element in the sample with rank $\lfloor s \cdot k/n \rfloor$.
  • Partition the array around $p$.
  • Let the rank of $p$ be $r$. If $|r - k| \le \sqrt{k \log n}$, then we have the element in the small interval and we can find it by sorting the interval? But the interval size is $|r - k|$, which is $O(\sqrt{k \log n})$. However, the expected $|r - k|$ is $O(n/\sqrt{s}) = O(n^{3/4})$, which is not $O(\sqrt{k \log n})$. So this doesn't work.

Maybe they use a two-stage sample: first a large sample to get a rough pivot, then a smaller sample within the small part? That would give a geometric series of sample sizes. The total error would be dominated by the largest sample size, which is $n^{2/3}$ if balanced. To get $\sqrt{n}$, they might use a different approach: they use a sample of size $s = \sqrt{n \log n}$, but they don't sort the sample; they use it to estimate the rank and then do a different kind of partitioning.

Let's think about the hint: "find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements". This is exactly the gap used in the Karloff-Raghavan paper. They call it the "target interval". They then say "after which the $t$th is easily located". This means once we have the exact $k_1$th and $k_2$th elements, we can find the $t$th by a simple selection among the $O(\sqrt{t \ln n})$ elements between them. So the algorithm must find the exact $k_1$th and $k_2$th elements. How?

Perhaps the algorithm finds the $k_1$th and $k_2$th by using a "tournament" that naturally identifies them? Or maybe the algorithm is a non-constructive proof using the fact that the optimal decision tree for finding the $t$th element also identifies the $k_1$th and $k_2$th?

Let's consider the possibility that the exercise is from Knuth's book and the hint is from Knuth himself. Knuth might have a specific algorithm in mind that is not the Karloff-Raghavan one. The hint says "Show that with this many comparisons we can in fact find both...". This is a constructive proof. The algorithm might be:

  1. Use a "tree selection" method but with a different tree structure that on average requires only $n + t$ comparisons.
  2. The "tree selection" method in Section 5.2.3 builds a binary tree and then finds the top $t$ elements. The worst-case is $n + t \log n$. But on a random permutation, the average number of comparisons might be $n + t + O(\sqrt{n \log n})$? Let's check: In tree selection, we build a knockout tournament tree. The comparisons are fixed: $n-1$ to build the tree. Then we find the second, third, etc. by comparing the opponents of the previously found elements. The number of opponents is the depth of the tree. In a balanced tree, depth is $\lceil \log n \rceil$. The number of comparisons to find the $i$th element is at most the depth. So total is $n + t \log n$. That's not $n + t$.

What if the tree is not balanced? The text says "tree selection" uses a balanced tree? Actually, the text says "tree selection sorting (see Section 5.2.3, Fig. 23)". That's a tournament tree. The number of comparisons to find the $t$th largest is $n + t \log n$ in the worst case. The average-case might be different if the tree is not fixed? If we build the tree adaptively based on outcomes? The text mentions "tree selection" as a method to find the maximum and then the second maximum, etc. It's a fixed tree.

Maybe the algorithm is: "Use a heap of size $t$"? We already saw that's $n + t \log t$.

Wait, there is an algorithm by "Floyd and Rivest" called "SELECT" that uses a sample. The paper is from 1975. The exercise in Knuth might be based on that paper. The hint might be from the paper. The paper uses a sample of size $n^{2/3} \log^{1/3} n$ and gives error $O(n^{2/3} \log^{1/3} n)$. The exercise asks for $O(\sqrt{n \log n})$, which is a better error term. Maybe there is a simpler algorithm that achieves $O(\sqrt{n \log n})$ by using a different technique, such as "pairwise comparisons" and "entropy"?

Let's search for "n + t + O(sqrt(n log n))" in the context of Knuth's exercises. I recall that in TAOCP, there is an exercise about the average number of comparisons for finding the median, and the answer is $1.5n + O(\sqrt{n \log n})$ or something. Exercise 23 asks for the asymptotic value of $V_{\lceil n/2 \rceil}(n)$ (worst-case). Exercise 24 asks for $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$. This might be a known result from a paper by "K. J. Supowit" or "R. W. Floyd"?

Maybe the algorithm is:

  • Use a "binary search" on the values by comparing elements to a randomly chosen pivot? No.

Another thought: The bound $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$ might be proven by a simple probabilistic argument: The expected number of comparisons of a certain randomized algorithm is at most this. The algorithm could be: "Pick a random element, use it as a pivot to partition the array. If the pivot is the $t$th, done. Otherwise, recurse on the appropriate side. But choose the pivot from a random sample of size $\sqrt{n \log n}$?" We already analyzed that and got $n^{2/3}$.

What if the algorithm uses a "tournament" that is not a fixed tree but a "selection network" that on average performs well? There's a concept of "optimal expected comparisons" for selection. The minimum expected number of comparisons to find the $t$th largest is known to be $n + \min(t, n-t) + o(n)$? Actually, there is a lower bound of $n + \min(t, n-t) - 2$? I'm not sure.

Let's try to derive the bound from the hint. The hint says: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." This is a statement about the existence of an algorithm. We can try to construct such an algorithm.

Algorithm:

  1. If $t$ is small (say $t \le \sqrt{n \ln n}$), use a simple algorithm (e.g., build a max-heap of size $t$) and bound the expected comparisons by $n + O(\sqrt{n \log n})$.
  2. For larger $t$, let $k_1 = t - \sqrt{t \ln n}$, $k_2 = t + \sqrt{t \ln n}$.
  3. Choose a random sample $S$ of size $s = \lceil \sqrt{n \ln n} \rceil$? No, we need to adjust $s$ based on $t$. Let $s = \lceil n^2 / (t \ln n) \rceil$. But if $s > n$, we just use the whole array? If $t < n/\ln n$, then $s > n$, so we can just sort the whole array? Sorting takes $O(n \log n)$ comparisons, which is larger than $n + t + O(\sqrt{n \log n})$. So we can't sort.

Maybe the algorithm uses a different approach for small $t$: For $t \le \sqrt{n}$, the bound is $n + O(\sqrt{n \log n})$. We can achieve this by using a tournament tree that finds the maximum, then the second maximum, etc., up to $t$, but we stop the tournament early? No, tournament tree takes $n + t \log n$. For $t = \sqrt{n}$, that's $n + \sqrt{n} \log n$, which is $n + O(\sqrt{n \log n})$. So for $t \le \sqrt{n}$, the tournament tree actually gives the bound! Because $t \log n \le \sqrt{n} \log n = O(\sqrt{n \log n})$. For $t$ larger than $\sqrt{n}$, the tournament tree gives $n + t \log n$, which is larger than $n + t + O(\sqrt{n \log n})$. So for large $t$, we need a different algorithm.

For large $t$ (say $t > \sqrt{n}$), we can use the sample-based algorithm with sample size $s = n/\log n$? That gives error $O(n/\log n)$? No.

Wait, if we use the sample-based algorithm with $s = \sqrt{n \ln n}$, the partition cost is $n + \min(k_1, n - k_2) \le n + t$. The cost to find pivots in the sample is $O(s) = O(\sqrt{n \ln n})$. The middle part size is $O(\sqrt{t \ln n} + n/\sqrt{s})$. $n/\sqrt{s} = n / (n^{1/4} (\ln n)^{1/4}) = n^{3/4} / (\ln n)^{1/4}$. For $t$ large, say $t = n/2$, $\sqrt{t \ln n} = \sqrt{n \ln n}/ \sqrt{2} \approx n^{1/2} (\ln n)^{1/2}$. The middle part size is dominated by $n^{3/4} / (\ln n)^{1/4}$, which is much larger than $\sqrt{n \ln n}$. So the middle part is large. Then finding the exact $k_1$th and $k_2$th within the middle part would cost at least the size of the middle part, which is $\Omega(n^{3/4})$. That's too large.

So the sample size must be chosen to make the middle part size $O(\sqrt{t \ln n})$. That requires $n/\sqrt{s} = O(\sqrt{t \ln n})$, i.e., $s = \Omega(n^2/(t \ln n))$. For $t = n/2$, $s = \Omega(n/\ln n)$. The cost to find the pivots in the sample is $O(s) = O(n/\ln n)$. The total cost is $n + t + O(n/\ln n)$. For $t = n/2$, $n/\ln n$ is much smaller than $\sqrt{n \log n}$? Wait, $n/\ln n$ grows faster than $\sqrt{n \log n}$ for large $n$. So $O(n/\ln n)$ is not $O(\sqrt{n \log n})$. It's larger. So this doesn't meet the bound.

But the bound is $O(\sqrt{n \log n})$, which is smaller than $n/\ln n$ for large $n$. So we need a sample size that is $O(\sqrt{n \log n})$ to keep the pivot-finding cost within the bound, but we also need the deviation $n/\sqrt{s}$ to be $O(\sqrt{t \ln n})$. If $s = \sqrt{n \log n}$, then $n/\sqrt{s} = n^{3/4} / (\log n)^{1/4}$, which is larger than $\sqrt{t \ln n}$ for $t \le n/2$? For $t = n/2$, $\sqrt{t \ln n} = \sqrt{n \ln n}/ \sqrt{2} \approx n^{1/2} (\ln n)^{1/2}$. Compare $n^{3/4} / (\ln n)^{1/4}$ vs $n^{1/2} (\ln n)^{1/2}$. The ratio is $n^{1/4} / (\ln n)^{3/4}$, which grows with $n$. So the deviation is larger. So the middle part is large.

Is there a way to reduce the middle part size without increasing the sample size? Maybe we don't need the middle part to be small; we just need to be able to find the exact $k_1$th and $k_2$th within it efficiently. If the middle part is large, we could recursively apply the same algorithm on the middle part? But then the recurrence would be $T(n) = n + t + T(\text{middle size})$. If middle size is $n^{3/4}$, then $T(n) = n + t + n^{3/4} + ...$ which sums to $O(n)$. That would be $n + t + O(n)$, not $O(\sqrt{n \log n})$.

The hint says "after which the $t$th is easily located". This implies that the middle part is small, i.e., size $O(\sqrt{t \ln n})$. So we must have $n/\sqrt{s} = O(\sqrt{t \ln n})$. Combined with $s = O(\sqrt{n \log n})$, we get $n/\sqrt{\sqrt{n \log n}} = O(\sqrt{t \ln n}) \implies n^{3/4} / (\log n)^{1/4} = O(\sqrt{t \ln n})$. For $t = n/2$, $\sqrt{t \ln n} = \Theta(\sqrt{n \log n})$. So we need $n^{3/4} / (\log n)^{1/4} = O(\sqrt{n \log n}) \implies n^{3/4} = O(n^{1/2} (\log n)^{3/4}) \implies n^{1/4} = O((\log n)^{3/4})$, which is false. Therefore, we cannot simultaneously have $s = O(\sqrt{n \log n})$ and $n/\sqrt{s} = O(\sqrt{t \ln n})$ for $t = \Theta(n)$. The only way is if the algorithm does not use a single sample and single partition. It must use a more sophisticated method, perhaps with multiple levels of sampling where the sample sizes decrease geometrically? That would give a sum of errors that is dominated by the first level? If we use multiple levels, the total error might be the sum of $n/\sqrt{s_i}$ plus sum of $s_i$. To minimize the sum, we set $s_i$ to balance, leading to $n^{2/3}$. So multiple levels don't help to get $\sqrt{n}$; they give $n^{2/3}$.

Wait, maybe the algorithm uses a sample of size $s = \sqrt{n \log n}$ and then does something else that reduces the error without needing a second partition? What if we use the sample to estimate the rank, and then we only compare the elements that are "close" to the estimated rank? We can't do that without comparing them to the sample.

Another idea: The algorithm might use a "tournament" that is built in a way that the $t$th largest is found by a path of length $n + t$ on average? For example, if we build a binary search tree by inserting elements in random order, the expected depth of the $t$th largest is $O(\log n)$. But we need $n + t$ comparisons, which is much larger than $\log n$. That's for finding a specific element in a sorted structure, but we are finding the $t$th largest among unsorted elements.

Let's think about the phrase "tree selection" in the context. The text says: "In fact, by Eq. 5.3.1-(3), the right-hand side of (6) equals $B(n)$ when $t = n$, and also when $t = n - 1$; hence tree selection and binary insertion yield the same upper bound for the sorting problem, although they are quite different methods." Then it describes the tree selection process with external nodes and internal nodes, and the multiset $\mu(\alpha)$ of distances. This is about worst-case. The exercises 21-23 are about worst-case bounds. Exercise 24 is about average-case $\overline{V}_t(n)$. The hint might be from a paper by "Kirkpatrick" or "Ramanan"? Exercise 21 is by Ramanan and Hyafil. Exercise 22 by Kirkpatrick. Exercise 23 is an open problem? Exercise 24 is HM40.

Maybe the algorithm for average-case is a simple one: "Repeatedly find the maximum and remove it"? That takes $n + (n-1) + ... + (n-t+1) = tn - t(t-1)/2$. For $t=1$, $n-1$. For $t=n/2$, $\approx n^2/4$. That's too large.

What about "Quickselect" but with a different analysis that shows the expected number of comparisons is $n + t + O(\sqrt{n \log n})$? Let's compute the expected number of comparisons for Quickselect to find the $t$th largest when $t \le n/2$. The standard Quickselect picks a random pivot, partitions, and recurses. The expected number of comparisons $C(n,t)$ satisfies: $C(n,t) = n-1 + \frac{1}{n} \sum_{i=1}^t C(n-i, t-i) + \frac{1}{n} \sum_{i=t+1}^n C(i-1, t)$. This is a known recurrence. The solution is $C(n,t) = 2n + O(t \log n)$? Actually, for $t=1$, $C(n,1) = n-1 + \frac{1}{n} \sum_{i=2}^n C(i-1,1)$. This gives $C(n,1) = 2n - H_n - 1 \approx 2n$. Wait, for $t=1$, Quickselect is just finding the minimum by picking a random pivot and partitioning? No, if you pick a random pivot and partition, the minimum is in the left part unless the pivot is the minimum. The standard Quickselect for minimum: pick pivot, partition, then recurse on the left part. The expected number of comparisons is $n-1 + \frac{1}{n} \sum_{i=1}^n C(i-1,1)$. This solves to $C(n,1) = 2n - 2H_n - 1 \approx 2n$. But we can find the minimum in $n-1$ comparisons deterministically. So Quickselect is not optimal for small $t$. The optimal algorithm for small $t$ is to just do a linear scan. So $\overline{V}_1(n) = n-1$. For $t=2$, there is an algorithm that does $n + \lceil \log n \rceil - 1$ comparisons worst-case (Schreier-Kislitsyn). The average-case might be $n + O(\log n)$? The bound $n + t + O(\sqrt{n \log n})$ for $t=2$ is $n + 2 + O(\sqrt{n \log n})$, which is larger than $n + \log n$, so it's trivially true.

For large $t$, say $t = n/2$, the bound is $1.5n + O(\sqrt{n \log n})$. Is there an algorithm that finds the median in $1.5n + o(n)$ expected comparisons? The Floyd-Rivest algorithm gives $1.5n + O(n^{2/3} \log^{1/3} n)$. The best known average-case for median is $1.5n + o(n)$? I think the lower bound for median is $1.5n$? Actually, the minimum expected number of comparisons to find the median is $(3/2)n + o(n)$? There's a result by "Dor and Zwick" (1999) "Median selection requires $(2 + \epsilon)n$ comparisons"? No, that's worst-case. For average-case, the median can be found in $1.5n + o(n)$? I recall a paper by "Karloff and Raghavan" (1990) "Randomized algorithms and expected time bounds for selection" that gives $n + \min(k, n-k) + O(\sqrt{n \log n})$. For $k = n/2$, this is $1.5n + O(\sqrt{n \log n})$. So that matches the exercise exactly! The Karloff-Raghavan algorithm achieves $n + \min(k, n-k) + O(\sqrt{n \log n})$. The exercise asks to prove $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$ for $t \le \lceil n/2 \rceil$. Since $\min(t, n-t) = t$ for $t \le n/2$, this is exactly the Karloff-Raghavan bound. The hint is from their paper. The hint says: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." This is exactly the key step in the Karloff-Raghavan algorithm: they find two elements that bracket the target rank, and then the target is easily found.

So the exercise is to prove the Karloff-Raghavan bound. I need to describe the algorithm and analyze it. The algorithm is:

  1. If $n$ is small, sort the array (constant comparisons).
  2. Let $k_1 = \lfloor t - \sqrt{t \ln n} \rfloor$, $k_2 = \lceil t + \sqrt{t \ln n} \rceil$.
  3. Choose a random sample $S$ of size $s = \lceil \sqrt{n \ln n} \rceil$.
  4. Find the $r_1$th and $r_2$th largest elements in $S$, where $r_1 = \lfloor s \cdot k_1 / n \rfloor$, $r_2 = \lceil s \cdot k_2 / n \rceil$. (We can find these by recursively applying the same algorithm on the sample, or by sorting the sample if $s$ is small? But $s = \sqrt{n \ln n}$, which might not be small enough to sort. We can use the same algorithm recursively on the sample to find its order statistics. This is a recursive algorithm.)
  5. Let $p$ be the $r_1$th largest in $S$, $q$ be the $r_2$th largest in $S$. (Note: $p > q$ since $r_1 < r_2$.)
  6. Compare $p$ and $q$ (1 comparison). If $p < q$, swap them.
  7. Partition the whole array with respect to $p$ and $q$ as follows: For each element $x$, first compare $x$ to $p$. If $x > p$, put it in set $L$. Else, compare $x$ to $q$. If $x < q$, put it in set $R$. Else, put it in set $M$ (where $q \le x \le p$).
  8. Let $l = |L|$, $m = |M|$, $r = |R|$. Note that $l + m + r = n$.
  9. The true $k_1$th and $k_2$th elements lie in $M \cup {p, q}$ with high probability. Specifically, $l$ is close to $k_1 - 1$, $r$ is close to $n - k_2$. The size of $M$ is $m \approx k_2 - k_1 + 1 + O(n/\sqrt{s})$. We need $m$ to be $O(\sqrt{t \ln n})$. For this, we need $n/\sqrt{s} = O(\sqrt{t \ln n})$. But $s = \sqrt{n \ln n}$ gives $n/\sqrt{s} = n^{3/4} / (\ln n)^{1/4}$, which is not $O(\sqrt{t \ln n})$ for $t = \Theta(n)$. So this doesn't work with $s = \sqrt{n \ln n}$.

Wait, maybe the sample size is not $\sqrt{n \ln n}$ but something else. In Karloff-Raghavan, they use a sample of size $s = \sqrt{n \log n}$? Let me check the paper mentally. I think the paper uses a sample of size $s = \sqrt{n \log n}$ and then uses a different partitioning strategy that doesn't rely on the deviation being small? Or maybe they use a sample of size $s = n / \sqrt{t \ln n}$? Let's check the numbers.

The hint says $\lfloor t - \sqrt{t \ln n} \rfloor$ and $\lceil t + \sqrt{t \ln n} \rceil$. The gap is $2\sqrt{t \ln n}$. The standard deviation of the rank of the $r$th sample element is $\sigma = n \sqrt{ \frac{r(s-r)}{s^3} } \approx n \sqrt{ \frac{t/n \cdot (1 - t/n)}{s} }$. For $t \le n/2$, $\sigma \approx n \sqrt{ \frac{t}{n s} } = \sqrt{ \frac{n t}{s} }$. We want $\sigma = O(\sqrt{t \ln n})$. This gives $\sqrt{ \frac{n t}{s} } = O(\sqrt{t \ln n}) \implies \frac{n}{s} = O(\ln n) \implies s = \Omega(n / \ln n)$. So the sample size must be at least $\Omega(n / \ln n)$. If $s = \Theta(n / \ln n)$, then the cost to find the order statistics in the sample is $O(s) = O(n / \ln n)$ if we use a linear-time selection algorithm on the sample? But we can find the $r_1$th and $r_2$th in the sample by recursively applying the same algorithm? That would lead to a recurrence. The total expected comparisons $C(n)$ satisfies $C(n) = n + t + C(s) + O(\sqrt{t \ln n})$. If $s = n / \ln n$, then $C(s) = s + t' + O(\sqrt{s \ln s})$. This sum might be $n + t + O(n / \ln n)$? But we need $O(\sqrt{n \log n})$. $n / \ln n$ is larger than $\sqrt{n \log n}$ for large $n$. So this doesn't give the bound.

Maybe the algorithm doesn't find the exact $r_1$th and $r_2$th in the sample by recursion, but instead sorts the sample? If $s = n / \ln n$, sorting costs $O(s \log s) = O(n)$, which is too large. If $s = \sqrt{n \ln n}$, sorting costs $O(\sqrt{n \ln n} \log n) = O(\sqrt{n} \log^{3/2} n)$, which is larger than $\sqrt{n \log n}$ by a factor of $\log n$. But maybe the $O(\sqrt{n \log n})$ bound allows a $\log n$ factor? The notation $O(\sqrt{n \log n})$ means there exists a constant $C$ such that $\le C \sqrt{n \log n}$. $\sqrt{n} \log^{3/2} n$ is not $O(\sqrt{n \log n})$ because $\log n$ factor grows. So sorting the sample is too expensive.

The Karloff-Raghavan algorithm uses a sample of size $s = \sqrt{n \log n}$ and then finds the $r_1$th and $r_2$th in the sample by a different method that costs $O(s)$ expected comparisons? But we saw that the deviation $\sigma = \sqrt{n t / s}$. With $s = \sqrt{n \log n}$, $\sigma = \sqrt{ \frac{n t}{\sqrt{n \log n}} } = (n t)^{1/2} / (n \log n)^{1/4} = n^{1/4} t^{1/2} / (\log n)^{1/4}$. For $t = n/2$, $\sigma = n^{3/4} / (\log n)^{1/4}$. The gap is $2\sqrt{t \ln n} = \sqrt{2 n \ln n} \approx n^{1/2} (\log n)^{1/2}$. The ratio $\sigma / \text{gap} = n^{1/4} / (\log n)^{3/4}$, which grows. So the middle part is large. How do they handle that? They might use a different gap size? The hint specifically says $\sqrt{t \ln n}$. Maybe they use a two-stage process: first a sample of size $s_1 = n / \ln n$ to get a rough pivot, then a sample of size $s_2 = \sqrt{n \log n}$ from the small part? That would give a total cost of $O(n / \ln n + \sqrt{n \log n})$, which is dominated by $n / \ln n$, not $\sqrt{n \log n}$.

I'm confused. Let me search for the exact Karloff-Raghavan result. I recall that Karloff and Raghavan (1990) gave an algorithm with expected comparisons $n + \min(k, n-k) + O(\sqrt{n \log n})$. The algorithm is:

  • Choose a random sample of size $s = \sqrt{n \log n}$.
  • Sort the sample.
  • Find the element in the sample with rank $r = \lfloor s \cdot k/n \rfloor$. Call it $p$.
  • Partition the array around $p$.
  • If the rank of $p$ is within $\sqrt{k \log n}$ of $k$, then we can find the $k$th element by sorting the appropriate subarray of size $O(\sqrt{k \log n})$.
  • Otherwise, we recurse on the larger part? But the expected size of the larger part is $n/2 + O(\sqrt{n \log n})$? No, if $p$ is the median of a sample of size $\sqrt{n \log n}$, its rank is concentrated around $n/2$ with standard deviation $n/\sqrt{s} = n^{3/4} / (\log n)^{1/4}$. So the probability that its rank is within $\sqrt{k \log n}$ of $k$ is very small unless $k$ is near $n/2$ and $\sqrt{k \log n}$ is large? For $k = n/2$, $\sqrt{k \log n} = \sqrt{n \log n}/ \sqrt{2}$. The standard deviation is $n^{3/4} / (\log n)^{1/4}$. The ratio is $n^{1/4} / (\log n)^{3/4}$, which is large, so the probability is not small; it's actually large? Wait, if the standard deviation is $\sigma$, the probability that the rank is within $c \sigma$ is constant. Here the gap is $O(\sqrt{n \log n})$, and $\sigma = n^{3/4} / (\log n)^{1/4}$. Since $\sqrt{n \log n} = o(\sigma)$ for large $n$, the gap is much smaller than the standard deviation. So the probability that the rank falls within the gap is very small (exponentially small). So that approach doesn't work.

Maybe the algorithm uses a sample of size $s = n / \log n$? Then $\sigma = n / \sqrt{s} = \sqrt{n \log n}$. The gap is $\sqrt{k \log n} \le \sqrt{n \log n}$. So the gap is comparable to $\sigma$. Then the probability that the pivot's rank is within the gap is constant. If we sort the sample, cost is $O(s \log s) = O(n \log n / \log n) = O(n)$. Partitioning costs $n$. If we get lucky, we are done with total $O(n)$. If not, we recurse? The expected number of trials would be constant. The total expected cost would be $O(n)$. But we need $n + t + O(\sqrt{n \log n})$. For $t = n/2$, $n + t = 1.5n$, so $O(n)$ is fine. But the constant factor might be larger than 1.5? The expected cost would be $c n$ for some $c > 1$. The bound $n + t + O(\sqrt{n \log n})$ has coefficient 1 on $n$ and coefficient 1 on $t$. For $t = n/2$, the coefficient on $n$ is 1.5. An algorithm with $c n$ would not satisfy the bound if $c > 1.5$. So the algorithm must have the exact leading terms $n + t$.

How can an algorithm have leading term $n + t$? The Floyd-Rivest algorithm achieves $n + \min(k, n-k) + o(n)$. The $n$ term comes from partitioning the whole array once. The $t$ term comes from the fact that the expected size of the recursive subproblem is $t + o(n)$? In Floyd-Rivest, the recursive subproblem is the middle part after partitioning with two pivots. The expected size of the middle part is $O(n^{2/3} \log^{1/3} n)$. The $t$ term comes from the fact that we don't pay $n$ again; we only pay the size of the middle part. But the first partition cost is $n + \min(k, n-k)$? Actually, in Floyd-Rivest, they use a three-way partition with two pivots. The expected number of comparisons for the partition is $n + \min(k, n-k) + O(1)$. Then the recursive call is on the middle part of size $O(n^{2/3} \log^{1/3} n)$, which costs $O(n^{2/3} \log^{1/3} n)$. So total = $n + \min(k, n-k) + O(n^{2/3} \log^{1/3} n)$. That's the Floyd-Rivest bound.

The Karloff-Raghavan bound improves the $O(n^{2/3})$ to $O(\sqrt{n \log n})$. How? They must use a different sample size or a different method to get the middle part size down to $O(\sqrt{n \log n})$. If the middle part size is $O(\sqrt{n \log n})$, then the recursive cost is $O(\sqrt{n \log n})$. The partition cost is $n + \min(k, n-k) + O(1)$. So total = $n + t + O(\sqrt{n \log n})$. So the key is to get the middle part size down to $O(\sqrt{n \log n})$. To do that, the pivots must be such that the true ranks are within $O(\sqrt{n \log n})$ of the targets. The standard deviation of the sample quantile is $n/\sqrt{s}$. So we need $n/\sqrt{s} = O(\sqrt{n \log n}) \implies \sqrt{s} = \Omega(\sqrt{n / \log n}) \implies s = \Omega(n / \log n)$. But then the cost to find the pivots in the sample is at least $s$ (if we use a linear-time selection algorithm on the sample) or $s \log s$ (if we sort). If $s = \Theta(n / \log n)$, then $s = \Omega(n / \log n)$, which is $\omega(\sqrt{n \log n})$. So the cost of finding the pivots would be larger than $\sqrt{n \log n}$. So how do they achieve $O(\sqrt{n \log n})$?

Maybe they don't find the pivots by sorting or selecting in the sample. They might use the sample in a different way. For example, they could use the sample to create a "sketch" and then do a binary search? Or they could use a "tournament" within the sample?

Wait, maybe the algorithm is not recursive. The hint says "Show that with this many comparisons we can in fact find both the $k_1$th and $k_2$th elements". This might be a direct algorithm that finds both in one pass without recursion. For instance, we can find the $k_1$th and $k_2$th by maintaining a set of candidates of size $k_2$ and using a tournament tree that only keeps the top $k_2$ elements? That would cost $n + k_2 \log k_2$. For $k_2 \approx t$, that's $n + t \log t$, not $n + t$.

What if we use a "bucket" approach? We can divide the elements into buckets and find the approximate rank?

Let's reconsider the hint. It says: "Prove that $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$ for $t \le \lceil n/2 \rceil$. Hint: Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located."

This is a typical style of Knuth's exercises: the hint describes the algorithm. The algorithm is:

  1. Find the $k_1$th and $k_2$th elements (where $k_1 = \lfloor t - \sqrt{t \ln n} \rfloor$, $k_2 = \lceil t + \sqrt{t \ln n} \rceil$).
  2. Then the $t$th element is among the elements between them (inclusive). The number of such elements is at most $k_2 - k_1 + 1 = O(\sqrt{t \ln n})$.
  3. Find the $t$th element among these $O(\sqrt{t \ln n})$ elements by sorting them or using a selection algorithm.

The total comparisons = (comparisons to find $k_1$th and $k_2$th) + (comparisons to find $t$th among the $O(\sqrt{t \ln n})$ elements). The second part is $O(\sqrt{t \ln n}) = O(\sqrt{n \log n})$. So we need to show that we can find the $k_1$th and $k_2$th with $n + t + O(\sqrt{n \log n})$ comparisons.

How to find both $k_1$th and $k_2$th? Maybe we can find them by a single run of an algorithm that finds the $k_1$th largest, and then the $k_2$th largest is easily found from the same data structure? For example, if we build a tournament tree that finds the $k_1$th largest, maybe the $k_2$th is nearby? But we saw tournament tree worst-case is $n + t \log n$.

What if we use a "binary search tree" built by inserting elements in random order? The expected number of comparisons to build a BST of $n$ elements is $O(n \log n)$. Not good.

Maybe the algorithm is: "Use a sample of size $\sqrt{n \ln n}$ to find the $k_1$th and $k_2$th exactly?" But we can't find exact order statistics from a sample without comparing to the whole array.

Wait, maybe the algorithm is:

  • Choose a random sample of size $s = \sqrt{n \ln n}$.
  • Sort the sample.
  • From the sample, pick the sample, we can estimate the values of the $k_1$th and $k_2$th elements. But we don't know their exact identities.
  • Then we can partition the whole array with respect to the sample elements? That would be a multi-way partition.

Another idea: The algorithm might be the "Floyd-Rivest" algorithm, but the exercise asks to prove a weaker bound (with $O(\sqrt{n \log n})$) which might be easier to prove than the original $O(n^{2/3} \log^{1/3} n)$ bound. The hint might be a simplification of the Floyd-Rivest proof. In Floyd-Rivest, they use a sample of size $s = n^{2/3} \log^{1/3} n$ and get error $O(n^{2/3} \log^{1/3} n)$. If we instead use a sample of size $s = \sqrt{n \log n}$, the error becomes $O(n/\sqrt{s}) = O(n^{3/4} / (\log n)^{1/4})$, which is larger than $\sqrt{n \log n}$. So that doesn't give the bound.

Maybe the algorithm uses a different approach: "Quickselect" but with a fixed pivot? No.

Let's think about the phrase "with this many comparisons we can in fact find both...". This might be a statement about the optimal decision tree for finding the $t$th element. The average depth of the optimal decision tree is $\overline{V}_t(n)$. There might be a known relation: $\overline{V}_t(n) \le n + t + O(\sqrt{n \log n})$. This could be proven by constructing a specific decision tree that achieves this bound. The hint tells us how to construct it: first find the two elements at ranks $k_1$ and $k_2$, then find $t$ between them. This is exactly the strategy of the Karloff-Raghavan algorithm, which is a randomized algorithm (or a decision tree with random choices). The expected number of comparisons is bounded.

I need to produce a rigorous proof. Since I'm not sure of the exact algorithm details, I should reconstruct the algorithm that meets the bound. The bound is $n + t + O(\sqrt{n \log n})$. I need to show that there exists an algorithm with that expected comparison count. The hint gives the structure: find the $k_1$th and $k_2$th, then $t$ is easy.

Let's try to design an algorithm that finds the $k_1$th and $k_2$th with $n + t + O(\sqrt{n \log n})$ expected comparisons. I'll use the following approach (inspired by Karloff-Raghavan):

Algorithm SELECT($n, t$):

  1. If $t \le 1$, return the maximum (for $t=1$) or second maximum using standard methods with $n-1$ or $n + \lceil \log n \rceil - 1$ comparisons. These are $n + O(\log n) \le n + t + O(\sqrt{n \log n})$.
  2. Let $k_1 = \lfloor t - \sqrt{t \ln n} \rfloor$, $k_2 = \lceil t + \sqrt{t \ln n} \rceil$.
  3. If $k_1 < 1$, set $k_1 = 1$. (For small $t$, the bound is trivial.)
  4. Choose a random sample $S$ of size $s = \lceil \sqrt{n \ln n} \rceil$. (Wait, we need $s$ such that the deviation is $O(\sqrt{t \ln n})$. As we saw, $s = \sqrt{n \ln n}$ doesn't work for $t = \Theta(n)$. So maybe $s$ depends on $t$? The hint doesn't specify the sample size. Maybe we choose $s = \lceil n / \ln n \rceil$? Then deviation is $\sqrt{n \ln n}$, which is $O(\sqrt{t \ln n})$ for $t = \Theta(n)$? For $t = n/2$, $\sqrt{t \ln n} = \sqrt{n \ln n}/ \sqrt{2}$, so deviation is of the same order. For smaller $t$, $\sqrt{t \ln n}$ is smaller, so deviation might be larger. But we can handle small $t$ by a different method (e.g., tournament).)
  5. Recursively find the $r_1 = \lfloor s \cdot k_1 / n \rfloor$th and $r_2 = \lceil s \cdot k_2 / n \rceil$th largest elements in $S$. Let these be $p$ and $q$.
  6. Compare $p$ and $q$ (1 comparison). If $p < q$, swap them. Now $p \ge q$.
  7. Partition the whole array into three sets: $L = {x > p}$, $M = {x : q \le x \le p}$, $R = {x < q}$. To do this efficiently, for each element $x$:
    • Compare $x$ with $p$. If $x > p$, put in $L$ (1 comparison).
    • Else, compare $x$ with $q$. If $x < q$, put in $R$ (2 comparisons).
    • Else, put in $M$ (2 comparisons). The number of comparisons is $n + |L|$ if we compare to $p$ first, or $n + |R|$ if we compare to $q$ first. We can choose the order based on which of $|L|$ and $|R|$ is expected to be smaller. Since $k_1 \le t \le n/2$, we expect $|L| \approx k_1 \le t$ and $|R| \approx n - k_2 \ge n/2$. So comparing to $p$ first gives expected comparisons $n + k_1 \le n + t$.
  8. Let $l = |L|$, $m = |M|$, $r = |R|$. The true $k_1$th and $k_2$th elements are in $M$ (or are $p$ and $q$). Specifically, the $k_1$th largest is the $(k_1 - l)$th largest in $M$, and the $k_2$th largest is the $(k_2 - l)$th largest in $M$? Wait, if $L$ contains elements larger than $p$, then the $l$ elements in $L$ are the top $l$ elements. The $(l+1)$th largest is the maximum of $M \cup {p}$? Actually, $p$ is in $M$? $p$ is the $r_1$th largest in the sample, but we don't know its exact rank in the whole array. It could be in $L$? No, we defined $L$ as elements strictly greater than $p$. $p$ itself is not in $L$. It is in $M$ if we include elements equal to $p$? But all elements are distinct (permutation). So $p$ is the element we selected. It is in $M$ because $q \le p \le p$. So $p \in M$. $q \in M$. The elements in $L$ are all greater than $p$. The elements in $M$ are between $q$ and $p$ inclusive. The elements in $R$ are less than $q$. The rank of $p$ in the whole array is $l+1$? Actually, if $L$ has $l$ elements, all greater than $p$, then $p$ is the $(l+1)$th largest. The rank of $q$ is $l + 1 + \text{number of elements in M between p and q} + 1$? More precisely, let $m' = |M \setminus {p,q}|$. Then the elements in $M$ are $p$, then some elements between $p$ and $q$, then $q$. The rank of $q$ is $l + m + 1$? If $M$ has $m$ elements, including $p$ and $q$, then the largest in $M$ is $p$ (rank $l+1$), the next is ..., the smallest is $q$ (rank $l+m$). So the rank of $q$ is $l + m$. We want the $k_1$th and $k_2$th elements. They are in $M$ if $k_1 \ge l+1$ and $k_2 \le l+m$. With high probability, this holds.
  9. Now we have a set $M$ of size $m$ that contains the $k_1$th and $k_2$th elements. We need to find the exact $k_1$th and $k_2$th largest in $M$. The target ranks within $M$ are $k_1' = k_1 - l$ and $k_2' = k_2 - l$. We can find them by recursively applying the same algorithm on $M$ with targets $k_1'$ and $k_2'$? But we need both, and the algorithm as described finds one target. We could find $k_1'$ and then $k_2'$? Or we could modify the algorithm to find two order statistics at once. The hint says "find both the $k_1$th and $k_2$th elements". Maybe we can find them by running the single-target algorithm twice? But that would double the cost. We need to find both with the same cost as finding one? Or maybe the algorithm for finding one target naturally identifies both? If we recursively apply the algorithm to find the $k_1'$th in $M$, we might get a set $M'$ that also contains $k_2'$? This is getting complicated.

Maybe the algorithm is not recursive. Perhaps we can find both $k_1$th and $k_2$th by a single pass that maintains a "window" of size $O(\sqrt{t \ln n})$ around the target? There is an algorithm called "Quickselect with a window" or "Partial Quicksort".

Let's look at the Karloff-Raghavan algorithm again. I found a summary in my memory: The algorithm SELECT($k$) finds the $k$th smallest.

  1. If $n$ is small, sort.
  2. Choose a random sample of size $s = \sqrt{n \log n}$.
  3. Sort the sample.
  4. Let $p$ be the element in the sample with rank $r = \lfloor s \cdot k/n \rfloor$.
  5. Partition the array around $p$.
  6. If the rank of $p$ is within $\sqrt{k \log n}$ of $k$, then the $k$th element is in a small subarray of size $O(\sqrt{k \log n})$. Sort this subarray and return the $k$th element.
  7. Otherwise, if $p$ is too small, recurse on the right part; if too large, recurse on the left part. The subarray size is $O(n)$? But the probability that the rank is far is small. They use a Chernoff bound to show that the expected cost is $n + k + O(\sqrt{n \log n})$.

Wait, if the rank of $p$ is not within $\sqrt{k \log n}$, then the subarray we recurse on could be of size $\Theta(n)$. The expected cost would be $n + C(n)$ again, leading to a recurrence $C(n) = n + p_{\text{bad}} C(n) + p_{\text{good}} O(\sqrt{k \log n})$. If $p_{\text{bad}}$ is small, we get $C(n) = n / (1 - p_{\text{bad}}) + ...$. But $p_{\text{bad}}$ is the probability that the sample quantile deviates by more than $\sqrt{k \log n}$. The standard deviation of the sample quantile is $\sigma = \sqrt{ \frac{k}{n} (1 - \frac{k}{n}) \frac{n^2}{s} } = \sqrt{ \frac{k(n-k)}{s} }$. For $s = \sqrt{n \log n}$, $\sigma = \sqrt{ \frac{k(n-k)}{\sqrt{n \log n}} }$. For $k = \Theta(n)$, $\sigma = \Theta( \sqrt{ n / \sqrt{n \log n} } ) = \Theta( n^{3/4} / (\log n)^{1/4} )$. The threshold is $\sqrt{k \log n} = \Theta( \sqrt{n \log n} )$. Since $\sqrt{n \log n} = o(\sigma)$, the probability of being within the threshold is extremely small! So $p_{\text{good}}$ is near 0, $p_{\text{bad}}$ is near 1. Then the recurrence gives $C(n) \approx n / p_{\text{good}} + ...$ which is huge. So this doesn't work.

Therefore, the sample size must be much larger. In Floyd-Rivest, $s = n^{2/3} \log^{1/3} n$, so $\sigma = \Theta( n^{2/3} / (\log n)^{1/6} )$, and the threshold is $\sqrt{k \log n} \approx n^{1/2} (\log n)^{1/2}$. The ratio $\sigma / \text{threshold} = n^{1/6} / (\log n)^{2/3}$, which still grows! Wait, in Floyd-Rivest, they use a different threshold? They use a gap of size $O(n/\sqrt{s})$? Actually, Floyd-Rivest uses two pivots to bracket the target. They choose two pivots from the sample such that the expected gap between them is $O(n/\sqrt{s})$. They set $s = n^{2/3} \log^{1/3} n$, so the gap is $O(n^{2/3} \log^{1/3} n)$. Then they partition and recurse on the middle part of that size. The cost of the partition is $n + O(\min(k, n-k))$. The recursive cost is the cost to select within the middle part. They use a different algorithm for the middle part? They use the same algorithm recursively? If they recurse, the middle part size is $O(n^{2/3})$, and the cost is $O(n^{2/3})$. So total $n + t + O(n^{2/3})$.

The Karloff-Raghavan improvement to $O(\sqrt{n \log n})$ must use a different technique. I recall that Karloff and Raghavan use a "tournament" or "heap" structure? Or maybe they use a "median of medians" type approach with a different group size?

Let's search for "Karloff Raghavan expected time bounds for selection". I remember the algorithm is called "SELECT" and it uses a sample of size $\sqrt{n \log n}$ and then does something like "binary search on the sample"? No.

Wait, maybe the hint in the exercise is exactly the Karloff-Raghavan algorithm, and I need to reproduce their proof. The hint says: "Show that with this many comparisons we can in fact find both the $\lfloor t - \sqrt{t \ln n} \rfloor$th and $\lceil t + \sqrt{t \ln n} \rceil$th elements, after which the $t$th is easily located." This is exactly the statement that the algorithm finds two elements that bracket the target. In the Karloff-Raghavan algorithm, they find the $k_1$th and $k_2$th by using a sample and then a partition. The analysis uses Chernoff bounds. The expected number of comparisons is $n + t + O(\sqrt{n \log n})$.

Given the time, I should construct a proof that follows the hint and uses standard probabilistic analysis. I'll assume the algorithm is:

  • For small $t$, use a simple algorithm.
  • For larger $t$, let $k_1 = t - \sqrt{t \ln n}$, $k_2 = t + \sqrt{t \ln n}$.
  • Choose a random sample $S$ of size $s = \lceil n / \ln n \rceil$? No, that would give $s \ln n = n$, so $s = n / \ln n$. Then sorting sample costs $O(n)$. Partition costs $n + t$. Middle part size $O(\sqrt{t \ln n} + n/\sqrt{s}) = O(\sqrt{t \ln n} + \sqrt{n \ln n})$. For $t = \Theta(n)$, this is $O(\sqrt{n \ln n})$. But the cost of sorting the sample is $O(n)$, which is larger than $O(\sqrt{n \log n})$. So we can't sort the sample. We need to find the pivots in the sample without sorting it. We can find the $r_1$th and $r_2$th in the sample using a linear-time selection algorithm (like median-of-medians) which takes $O(s)$ comparisons. If $s = n / \ln n$, then $O(s) = O(n / \ln n)$, which is still $\omega(\sqrt{n \log n})$. So we need $s = O(\sqrt{n \log n})$ to keep the pivot-finding cost within the bound. But then the deviation is too large.

Is there a way to find the pivots in the sample without sorting and without a full selection? Maybe we can just pick two random elements from the array as pivots? If we pick two random elements, the expected partition cost is $n + \min(\text{rank}_1, n - \text{rank}_2)$. If we want this to be $n + t$, we need $\min(\text{rank}_1, n - \text{rank}_2) \le t$. The probability that a random element has rank $\le t$ is $t/n$. So the probability that both pivots satisfy this is $(t/n)^2$? Not good.

Maybe the algorithm is:

  • Use a "tournament" to find the maximum, then the second maximum, etc., but only up to $t + \sqrt{t \ln n}$? The tournament to find the first $t + \sqrt{t \ln n}$ elements takes $n + (t + \sqrt{t \ln n}) \log n$ comparisons worst-case. The average-case might be $n + t + O(\sqrt{n \log n})$? Let's check: In a tournament tree, the number of comparisons to find the $i$th largest given the first $i-1$ is the number of opponents of the previously found elements that haven't been compared yet. In a balanced tree, this is $\log n$. So total $n + t \log n$. Not $n + t$.

What if the tournament is not balanced? There is a result by "K. J. Supowit" that the optimal tournament for average-case has a different shape. The average-case depth of a tournament tree built on a random permutation might be $O(\log n)$ per element, still $t \log n$.

Maybe the algorithm is the "Floyd-Rivest" algorithm, and the $O(\sqrt{n \log n})$ is a typo in my memory? The exercise says $O(\sqrt{n \log n})$. Knuth's exercises often have specific constants. The hint uses $\sqrt{t \ln n}$. This is very specific. I should trust the hint and try to prove the bound using the hint's structure, even if I'm not sure of the exact algorithm. The proof might be:

  • Define an algorithm that finds the $k_1$th and $k_2$th.
  • Show that the expected number of comparisons is $n + t + O(\sqrt{n \log n})$.
  • Use Chernoff bounds to bound the deviation of the sample quantile.
  • The sample size $s$ is chosen to balance the costs.

Let's try to set $s = \lceil \sqrt{n \ln n} \rceil$ and see if the analysis can be forced to $O(\sqrt{n \log n})$ by a more careful argument. Maybe the middle part size is not $O(\sqrt{t \ln n} + n/\sqrt{s})$ but $O(\sqrt{t \ln n})$ with high probability, and the probability of large middle part is exponentially small, and we can handle the large middle part by a fallback method that costs $O(n)$ but happens with exponentially small probability, contributing $O(1)$ to the expectation? If the middle part size is $O(\sqrt{t \ln n})$ with high probability, and with probability $p$ it is large (say $O(n)$), and we handle the large case by sorting the whole array ($O(n \log n)$ comparisons), the expected cost might still be $n + t + O(\sqrt{n \log n})$ if $p$ is small enough. But we need $p \cdot O(n \log n) = O(\sqrt{n \log n})$, so $p = O(\sqrt{n \log n} / (n \log n)) = O(1/\sqrt{n \log n})$. Is the probability of large deviation that small? For a sample quantile, the deviation is sub-Gaussian. The probability that the rank deviates by more than $k \sigma$ is $e^{-k^2/2}$. Here $\sigma = n/\sqrt{s} = n^{3/4} / (\ln n)^{1/4}$. We want the deviation to be at most $\sqrt{t \ln n} \le \sqrt{n \ln n}$. The ratio $k = \sqrt{n \ln n} / \sigma = n^{1/4} / (\ln n)^{3/4}$. The probability of deviation is $e^{-k^2/2} = e^{- \sqrt{n} / (\ln n)^{3/2} }$, which is super-exponentially small! Wait, if $\sigma = n^{3/4} / (\ln n)^{1/4}$ and the gap is $n^{1/2} (\ln n)^{1/2}$, then $k = n^{1/4} / (\ln n)^{3/4}$. The probability of deviation is $e^{-n^{1/2} / \ln^{3/2} n}$, which is incredibly small. So the middle part size is actually $O(\sqrt{n \ln n})$ with overwhelming probability! The expected cost of the fallback (which could be $O(n \log n)$) is negligible. So we can choose $s = \sqrt{n \ln n}$ and the middle part size is $O(\sqrt{n \ln n})$ with probability so close to 1 that the expected cost of the bad event is $O(1)$. Then the expected cost of the algorithm is $n + t + O(\sqrt{n \ln n}) + \text{negligible} = n + t + O(\sqrt{n \log n})$. This works! The key is that the deviation probability is extremely small, so we don't need the middle part to be small in expectation; we just need it to be small with overwhelming probability. The expected cost of handling the bad case is negligible.

Let's verify: $s = \sqrt{n \ln n}$. $\sigma = \sqrt{ \frac{k(n-k)}{s} } \approx \sqrt{ \frac{n/2 \cdot n/2}{\sqrt{n \ln n}} } = \sqrt{ \frac{n^2/4}{n^{1/2} (\ln n)^{1/2}} } = \frac{n}{2} / (n^{1/4} (\ln n)^{1/4}) = \frac{1}{2} n^{3/4} / (\ln n)^{1/4}$. The gap we want is $O(\sqrt{t \ln n}) \le \sqrt{n \ln n} = n^{1/2} (\ln n)^{1/2}$. The ratio $k = \frac{n^{1/2} (\ln n)^{1/2}}{n^{3/4} / (\ln n)^{1/4}} = \frac{1}{n^{1/4}} \cdot (\ln n)^{3/4} \to 0$ as $n \to \infty$! Wait, I miscalculated: $\sigma = n/\sqrt{s}$ for the rank? Actually, the rank of the sample quantile has variance $\frac{n^2}{s} \frac{r(s-r)}{s^2}$? Let's be precise.

We have a sample of size $s$. We pick the element with rank $r$ in the sample. The expected rank of this element in the whole array is $\mu = n \cdot r/s$. The variance is $\text{Var} = \frac{r(s-r)}{s+1} \cdot \frac{n^2 - s}{n-1} \approx \frac{r(s-r)}{s^2} n^2$. For $r = \alpha s$, $\text{Var} \approx \alpha(1-\alpha) n^2 / s$. So $\sigma = n \sqrt{\alpha(1-\alpha)/s}$. For $t = n/2$, $\alpha = 1/2$, $\sigma = n/(2\sqrt{s})$. With $s = \sqrt{n \ln n}$, $\sigma = n/(2 n^{1/4} (\ln n)^{1/4}) = \frac{1}{2} n^{3/4} / (\ln n)^{1/4}$. The gap we want is $\sqrt{t \ln n} = \sqrt{n \ln n}/ \sqrt{2} \approx n^{1/2} (\ln n)^{1/2}$. Compare gap to $\sigma$: gap / $\sigma = \frac{n^{1/2} (\ln n)^{1/2}}{n^{3/4} / (\ln n)^{1/4}} = n^{-1/4} (\ln n)^{3/4} \to 0$. So the gap is much SMALLER than the standard deviation! The probability that the rank falls within a gap smaller than $\sigma$ is about gap/$\sigma$, which is $O(n^{-1/4} (\ln n)^{3/4})$, which goes to 0. So the probability of being in the good interval is small, not large. I had the inequality reversed earlier. The standard deviation is LARGER than the gap, so the probability of being within the gap is small. That means the bad event (rank outside gap) happens with high probability. So we cannot rely on the gap being small with high probability. We need the gap to be larger than the standard deviation. So we need $\sigma \le \text{gap}$, i.e., $n/\sqrt{s} \le \sqrt{t \ln n}$. This requires $s \ge n^2/(t \ln n)$. For $t = n/2$, $s \ge 2n/\ln n$. So $s$ must be $\Omega(n/\ln n)$. That makes the cost of finding the pivots in the sample $\Omega(n/\ln n)$, which is larger than $\sqrt{n \log n}$.

So the single-sample approach cannot achieve $O(\sqrt{n \log n})$ with a fixed $s$. It must use a multi-level approach where the sample size decreases? But we already saw that gives $n^{2/3}$.

Wait, maybe the algorithm doesn't use a sample at all. The hint says "find both the $k_1$th and $k_2$th elements". Maybe we can find them by a tournament that naturally has $n + t$ average comparisons? Let's consider the following algorithm for finding the $t$th largest:

  • We can do a "binary search" on the values by comparing elements to a random pivot, but we keep track of the elements that are less than the pivot and greater than the pivot. This is Quickselect.
  • The expected number of comparisons for Quickselect to find the $t$th largest is $2n + o(n)$? For $t = n/2$, it's about $1.5n$? Actually, the exact expected number of comparisons for Quickselect (Hoare's algorithm) to find the median is about $1.5n$? Let's check: The recurrence is $C(n, n/2) = n-1 + \frac{2}{n} \sum_{i=n/2+1}^n C(i-1, n/2)$. This solves to about $1.5n$? I think the constant is 2 for extreme $k$, but for $k=n/2$, it's lower? Let's compute: For $k=n/2$, the expected size of the subarray is $\frac{1}{n} \sum_{i=n/2+1}^n (i-1) + \frac{1}{n} \sum_{i=1}^{n/2-1} (n-i) \approx \frac{1}{n} \cdot 2 \cdot \sum_{i=n/2}^{n} i \approx \frac{1}{n} \cdot 2 \cdot \frac{n^2 - (n/2)^2}{2} = \frac{3n}{4}$. So expected size is $3n/4$. Then $C(n) = n + 3n/4 + (3/4)^2 n + ... = 4n$. That's $4n$, not $1.5n$. Wait, that's for the standard Quickselect that picks a random pivot and then recurses on the appropriate side. The expected number of comparisons for Quickselect to find the median is about $3.39n$? I'm not sure. But it's definitely not $1.5n$.

The Floyd-Rivest algorithm achieves $1.5n + o(n)$ by using a sample to get a pivot that is very close to the median, so the subarray size is $o(n)$. The first partition costs $n$, and the second partition costs $o(n)$. So total $n + o(n)$. But we need $n + t = 1.5n$. So the Floyd-Rivest algorithm is $n + \min(k, n-k) + o(n)$, which for median is $1.5n + o(n)$. The error term is $O(n^{2/3} \log^{1/3} n)$. The exercise asks to prove $n + t + O(\sqrt{n \log n})$. This is a stronger result (smaller error term). Is it known to be true? I recall a paper by "Karloff and Raghavan" that improves the error to $O(\sqrt{n \log n})$. I think their algorithm uses a sample of size $s = \sqrt{n \log n}$ and a different analysis that somehow gets the error down. Maybe they use a "median of medians" on the sample? Or they use a tournament within the sample?

Let's think about the possibility that the algorithm is not sample-based but uses a "heap" of size $\sqrt{n \log n}$?

Given the time constraints, I need to produce a solution. I'll write a proof that follows the hint and uses a randomized algorithm with a sample of appropriate size, and uses the fact that the probability of large deviation is small enough to keep the expected cost within the bound. I'll set the sample size $s = \lceil \sqrt{n \ln n} \rceil$ and argue that the expected size of the middle part is $O(\sqrt{n \log n})$? But we saw that the standard deviation is larger than the gap, so the expected size of the middle part is actually large. However, maybe we can choose the pivots differently: instead of using the sample quantiles directly, we can use the sample to find two elements that are guaranteed to bracket the target with high probability? There is a technique: take a sample, sort it, and then pick the two elements that are at ranks $r_1$ and $r_2$ such that the interval between them contains the target with high probability. The size of this interval in the whole array is $O(\sqrt{n \log n})$? If we sort the sample of size $s$, the gaps between consecutive sample elements have expected size $n/s$. The maximum gap is $O(n \log s / s)$ with high probability. If we set $s = \sqrt{n \log n}$, then $n/s = \sqrt{n / \log n}$, which is much smaller than $\sqrt{n \log n}$. The maximum gap might be $O(\sqrt{n \log n})$? Let's check: The expected maximum gap in a random partition of $n$ elements by a random sample of size $s$ is about $n \log s / s$. For $s = \sqrt{n \log n}$, $n \log s / s = \sqrt{n \log n} \cdot \frac{\log s}{\sqrt{\log n}} \approx \sqrt{n \log n} \cdot \frac{\log n}{2 \sqrt{\log n}} = \frac{1}{2} \sqrt{n} \log^{3/2} n$, which is larger than $\sqrt{n \log n}$. But maybe we can choose the sample size adaptively.

I'm running out of time. I'll write a solution that outlines the algorithm and the probabilistic analysis, using the hint as the guide. I'll state the algorithm clearly and then prove the bound.

Algorithm:

  1. If $t$ is small (e.g., $t \le \sqrt{n \ln n}$), find the $t$th largest by building a max-heap of the first $t$ elements and scanning the rest. This takes $n + O(t \log t) \le n + O(\sqrt{n \ln n} \log n) \le n + O(\sqrt{n \log n})$ expected comparisons? Actually, for small $t$, we can just use the worst-case tournament method which takes $n + t \lceil \log n \rceil$ comparisons. For $t \le \sqrt{n \ln n}$, $t \log n \le \sqrt{n \ln n} \log n = O(\sqrt{n} \log^{3/2} n)$, which is not $O(\sqrt{n \log n})$. But we can use a better algorithm for small $t$: Quickselect with expected $2n$? That's $O(n)$, which is $n + O(n)$, not $n + O(\sqrt{n \log n})$. We need to be careful. Maybe we don't need to treat small $t$ separately; the bound is asymptotic and the algorithm works for all $t$ by choosing the gap appropriately.

Let's design the algorithm as follows (based on Karloff-Raghavan):

  • Let $k_1 = \max(1, \lfloor t - \sqrt{t \ln n} \rfloor)$, $k_2 = \min(n, \lceil t + \sqrt{t \ln n} \rceil)$.
  • Choose a random sample $S$ of size $s = \lceil \sqrt{n \ln n} \rceil$.
  • Sort $S$ completely. (Cost $O(s \log s) = O(\sqrt{n \ln n} \log n) = O(\sqrt{n} \log^{3/2} n)$. This is not $O(\sqrt{n \log n})$, but maybe we can find the needed order statistics in $S$ without fully sorting it? We can find the $r_1$th and $r_2$th largest in $S$ by a linear-time selection algorithm, which takes $O(s)$ expected comparisons. Then we don't need to sort $S$. The cost is $O(s) = O(\sqrt{n \ln n})$.)
  • Let $p$ be the $r_1$th largest in $S$, $q$ be the $r_2$th largest, where $r_1 = \lfloor s \cdot k_1 / n \rfloor$, $r_2 = \lceil s \cdot k_2 / n \rceil$.
  • Compare $p$ and $q$ (1 comparison). Assume $p > q$ (swap if not).
  • Partition the whole array with respect to $p$ and $q$ as described (compare to $p$ first, then to $q$ if needed). This takes $n + \min(|L|, |R|)$ comparisons. In expectation, $\min(|L|, |R|) \le t + O(\sqrt{n \log n})$? Actually, $\mathbb{E}[|L|] = k_1$, $\mathbb{E}[|R|] = n - k_2$. The minimum of the expectations is $\le t$. By Chernoff, $|L|$ and $|R|$ are close to their expectations with high probability. The probability that they deviate by more than $\sqrt{t \ln n}$ is small. The expected value of $\min(|L|, |R|)$ is $\le t + O(\sqrt{n \log n})$? Wait, if $|L|$ is concentrated around $k_1$, and $k_1 \le t$, then $\mathbb{E}[\min(|L|, |R|)] \le k_1 + O(\sqrt{n \log n}) \le t + O(\sqrt{n \log n})$.
  • Let $M$ be the middle set. Its size $m = |M|$. With high probability, $m = O(\sqrt{t \ln n} + n/\sqrt{s})$. But we saw $n/\sqrt{s}$ is large. However, the probability that $m$ is large is small? No, the expected $m$ is large because the standard deviation is large. But we don't need $m$ to be small in expectation; we need the total expected cost of finding the exact $k_1$th and $k_2$th within $M$ to be small. If we sort $M$, the cost is $m \log m$. The expected $m$ might be large, making the expected cost large. But if we use a different algorithm for the bad case, we might control the expectation.

Maybe we can do the following: If $m$ is larger than some threshold, we just run a deterministic linear-time selection algorithm on the whole array? That would cost $O(n)$ comparisons. The probability of large $m$ is the probability that the sample quantiles deviate significantly. By Chernoff, this probability is $\exp(-\Omega(\text{gap}^2 / \sigma^2))$. We need to choose the gap such that this probability times $O(n)$ is $O(\sqrt{n \log n})$. If we set the gap to be $c \sqrt{t \ln n}$ with a large constant $c$, then the deviation probability is $e^{-c^2 \ln n} = n^{-c^2}$. The expected cost of the fallback (e.g., $O(n)$) is $n \cdot n^{-c^2} = n^{1-c^2}$. If $c^2 \ge 1.5$, this is $O(n^{-0.5})$, negligible. But we also need the gap size to be $O(\sqrt{t \ln n})$ to keep the cost of processing $M$ small. So we set $k_1 = t - c \sqrt{t \ln n}$, $k_2 = t + c \sqrt{t \ln n}$ for a sufficiently large constant $c$. Then the expected size of $M$ is $O(\sqrt{t \ln n})$, and the probability of deviation is exponentially small, making the fallback cost negligible. The cost of finding $p$ and $q$ in the sample is $O(s)$ if we use Quickselect on the sample. The sample size $s$ must be large enough that the standard deviation $\sigma = n/\sqrt{s}$ is $O(\sqrt{t \ln n})$. So we need $s = \Omega(n^2/(t \ln n))$. For $t = \Theta(n)$, $s = \Omega(n/\ln n)$. The cost to find $p$ and $q$ in the sample is $O(s) = O(n/\ln n)$. This is $\omega(\sqrt{n \log n})$ for large $n$. So the cost of the sample processing dominates and is larger than $O(\sqrt{n \log n})$.

This is the fundamental barrier: to make the deviation small, $s$ must be large, making sample processing expensive. To make sample processing cheap, $s$ must be small, making deviation large. The trade-off gives $n^{2/3}$ for the minimum of $s + n/\sqrt{s}$. To get $\sqrt{n}$, we need to break this trade-off. How? By using a multi-level sampling where the sample sizes decrease geometrically?