---
title: "Rolling Min Tree: Fixed-Size Heap Top-K Selection at Billion-Element Scale"
categories:
- Guides
- C
---
Many applications only need the *K* largest elements of a vector of *N*
values. Sorting the vector and keeping the first *K* costs O(*N* log *N*) time
and O(*N*) extra memory. This work analyzes the *Rolling Min Tree* (RMinT), a
fixed-capacity min-heap of size *K* that discards its root when a larger
element arrives, and compares it with complete sorting. RMinT costs
O(*N* log *K*) in the worst case, O(*N* + *K* log *K* log(*N*/*K*)) on average
for random input, and uses O(*K*) memory. Time is measured on a single core of
FinisTerrae III (CESGA) with *N* from 10⁴ to 10⁹ and *K* from 1 to 10⁶. With
random input and *N* = 10⁹, RMinT is 259× faster than sorting for *K* ≤ 10⁴
and 79× faster for *K* = 10⁶. The advantage grows with *N*.
*By Hugo Coto Flórez and David Sanchez Miguez.*
[Download the paper](https://github.com/hugoocoto/RMinT/releases/download/latest/paper_en.pdf)
(PDF, also in [Spanish](https://github.com/hugoocoto/RMinT/releases/download/latest/paper.pdf)).
## Introduction
Finding the *K* largest values of a set (*top-K*) is a common step in
statistics and machine learning. After any transformation of the data, many
tasks need only those *K* values. Sorting the whole vector does far more work
than necessary. The *Rolling Min Tree* (RMinT) performs this selection using a
binary min-heap [^williams1964] of fixed capacity *K*.
## The Rolling Min Tree
RMinT stores up to *K* real values in a min-heap: the children of node *i* are
at 2*i* + 1 and 2*i* + 2, and its parent at ⌊(*i* − 1)/2⌋. The root is the
minimum of the stored values. Each insertion has two cases:
1. **Tree not full.** The element is placed at the end and moves up while it
is smaller than its parent.
2. **Tree full.** If the element is less than or equal to the root, it is
discarded with a single comparison. Otherwise it replaces the root and sifts
down: one sift-down per insertion.
After processing the first *i* elements, the tree holds the min(*i*, *K*)
largest of them, and the root is the *K*-th largest seen so far: nothing below
it can enter the result. At the end, the tree holds the *K* largest elements,
unsorted.
## Cost analysis
**Memory.** RMinT uses *K* values, O(*K*). Complete sorting processes all *N*
elements and, if the original vector is preserved, requires additional space
proportional to *N*.
**Worst case.** Each element causes at most one sift-down or one sift-up in a
heap of height ⌊log₂ *K*⌋: O(*N* log *K*). It happens with ascending input,
where every element is larger than all stored ones and always enters. It is
measured only as a reference.
**Random case.** The *i*-th element (*i* > *K*) is among the *K* largest of the
first *i* with probability *K*/*i*, so the expected number of insertions is
> *K* + Σ*i*=*K*+1*N* *K*/*i* ≈ *K*(1 + ln(*N*/*K*)),
and the expected cost is O(*N* + *K* log *K* · log(*N*/*K*)). With *K* ≪ *N*,
*N* dominates: almost every element is discarded with one comparison against
the root, in a sequential scan.
**Sorting.** Complete sorting performs Θ(*N* log *N*) comparisons
[^knuth1998], regardless of *K*.
## Methodology
RMinT is compared with complete sorting in descending order.
- **Data.** Uniform values in [0, 1) with a fixed seed; ascending input *i*/*N*
is also tested (worst case, only as a reference).
- **Sizes.** *N* ∈ {10⁴, …, 10⁹} and *K* ∈ {1, 10, …, 10⁶} with *K* < *N*.
- **Time.** Median of 5 repetitions (3 for *N* = 10⁹).
- **Correctness.** After each run, the *K* tree elements, sorted, match the
first *K* of the sorted vector. All checks passed.
- **Platform.** One core of a shared FinisTerrae III node [^ft3guide], with an
Intel Xeon Platinum 8352Y at 2.2 GHz.
## Results
The figure below shows time versus *N* with random input. With *K* ≤ 10³,
RMinT takes 0.84 ns per element, almost constant: the cost of reading the
element and comparing it with the root. The cost of complete sorting rises from
105 ns (*N* = 10⁴) to 222 ns (*N* = 10⁹), as expected from a log *N* factor.

The next figure shows the speedup *t*sort/*t*RMinT. It is
above 1 in all cases (minimum 3.3×, with *K* = 10⁶ and *N* = 10⁷) and grows
with *N*: with *N* = 10⁹ it reaches 259× for *K* ≤ 10⁴ and drops to 79× with
*K* = 10⁶, where about *K*(1 + ln(*N*/*K*)) ≈ 7.9 · 10⁶ insertions are
expected, under 1 % of the elements.

Median time with *N* = 10⁹:
| Method | Random (ms) | Ascending (ms) |
|---|--:|--:|
| Sorting | 222 334 | 56 199 |
| RMinT, *K* = 1 | 844 | 888 |
| RMinT, *K* = 10² | 837 | 18 359 |
| RMinT, *K* = 10⁴ | 856 | 100 352 |
| RMinT, *K* = 10⁶ | 2 802 | 105 383 |
With *N* = 10⁹ and random input (table above and figure below), time is flat
up to *K* = 10⁴ and triples at *K* = 10⁶. With ascending input every element
enters the tree and the cost becomes O(*N* log *K*): for *K* ≥ 10³ RMinT is
slower than complete sorting, which is also 4.0× faster than on random data.
This case is not the intended use.

The spread between repetitions is 0.9 % in median and at most 4 % for
*N* ≥ 10⁸.
## Algorithmic properties and extensions
**Selection invariant.** After reading any input prefix, the tree contains
exactly the min(*i*, *K*) largest values in that prefix, including duplicates.
The property is preserved because, when the tree is full, its root is the
smallest current candidate: a value that does not exceed it cannot belong to
the largest *K*, while a larger value replaces a candidate that may leave the
set. The subsequent sift-down restores the heap property without changing the
selected set.
The result is unsorted, which is useful when only the threshold or the *K*
values themselves are needed. If sorted output is required, repeatedly
extracting the root costs O(*K* log *K*), independently of *N*. In a
distributed execution, each process can retain its local *K* largest values:
an element discarded locally cannot be among the global *K* largest. Combining
those candidates with another RMinT or a balanced reduction therefore produces
the global result without gathering all *N* values.
## Conclusions
RMinT solves the selection of the *K* largest elements with O(*K*) memory and,
for random data, a cost that is practically linear in *N*. With *N* = 10⁹ it
takes 0.84 s versus 222 s for complete sorting, and its advantage grows with
*N*. Against Hoare's expected-O(*N*) quickselect [^hoare1961], RMinT preserves
input and uses O(*K*) state, avoiding full-array reordering. If *N* exceeds
RAM, quickselect cannot partition directly; RMinT streams chunks, retaining *K*
values with sequential I/O, and can remain performant. This case is unmeasured.
## See also
- [Source code](https://github.com/hugoocoto/RMinT) (MIT)
- [cesga-ft3-skill](https://github.com/hugoc10/cesga-ft3-skill), for running jobs on FinisTerrae III
---
[^williams1964]: J. W. J. Williams, "Algorithm 232: Heapsort", *Communications of the ACM*, 7(6), 347–348, 1964.
[^knuth1998]: Donald E. Knuth, *The Art of Computer Programming, Vol. 3: Sorting and Searching*, 2nd ed., Addison-Wesley, 1998.
[^ft3guide]: CESGA, [FinisTerrae III User Guide](https://cesga-docs.gitlab.io/ft3-user-guide/), 2026.
[^hoare1961]: C. A. R. Hoare, "Algorithm 65: Find", *Communications of the ACM*, 4(7), 321–322, 1961.