Selecting a Range from Ordered Records

A range query uses ordered keys to find its start and then returns only the items before its stop boundary.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace the endpoints
  2. Account for duplicates
  3. Use two lower bounds for a half-open interval
  4. Budget the cost of emitting results separately

Trace the endpoints

For keys 2, 5, 5, 7, 11, the range \([5,11)\) starts at the first 5. It emits 5, 5, and 7 and stops before 11. Boundary search can find the first admissible position.

Account for duplicates

Finding an arbitrary 5 may skip its earlier copy. Define the contract in terms of the earliest admissible index or ordered iterator. Half-open slice boundaries describe how adjacent ranges can meet without duplication.[1]

Use two lower bounds for a half-open interval

On keys [2, 4, 4, 8, 12], selecting \([4,12)\) starts at index one and stops at index four. Both fours are included and twelve is excluded. The output length is the difference of boundaries. See ordered list search for computing the two positions without scanning every outside value.

First key ≥ 4: index 1
First key ≥ 12: index 4
Selected indices: 1, 2, 3
Keys: 4, 4, 8

Budget the cost of emitting results separately

Locating the boundaries can take logarithmic comparisons on a sorted random-access array. Returning \(k\) entries still involves enumerating those \(k\) entries. Define the policy for a reversed interval and test duplicate keys exactly at both endpoints.

References

  1. Jeff Erickson, Algorithms ↩