Skip to content

Rank and Select

Rank and select are order-statistic operations on a collection that is not stored in sorted order.

Rank returns an element’s position in the sorted view, and select returns the element at a given position in that sorted view.

Rank

Given an unsorted array of \(n\) elements, rank(x) returns the number of numbers smaller than x.

By Partitioning

Running the partition step of quicksort using \(x\) as pivot, will immediately tell you its rank.

Time Complexity: \(O(n)\)

Using an OST

  • Preprocessing: \(O(n \log n)\)
  • Per query: \(O(\log n)\)

Select

select(k) Returns the element at position k in sorted order.

Using QuickSelect

Running QuickSelect takes expected \(O(n)\) time.

Using an OST

The time required is the same as it's rank operation.

  • Preprocessing: \(O(n \log n)\)
  • Per query: \(O(\log n)\)