C#: an ordered index with SortedSet
A database index that keeps keys in order can answer range queries and nearest-value queries without scanning every row. SortedSet<T> is a collection of unique values stored in a balanced tree, so adding, removing, and finding take O(log n).
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var ages = new SortedSet<int> { 31, 24, 45, 29 };
Console.WriteLine("min " + ages.Min + " max " + ages.Max);
Console.WriteLine("range 25..40: " + string.Join(" ", ages.GetViewBetween(25, 40)));
int floor = ages.GetViewBetween(int.MinValue, 30).Max;
int ceiling = ages.GetViewBetween(30, int.MaxValue).Min;
Console.WriteLine("floor(30)=" + floor + " ceiling(30)=" + ceiling);
}
}