Input: A set S of n elements over a totally ordered universe.
Output: The median element of S, denoted by m.
- Pick a (multi-)set R of
elements in S, chosen independently and uniformly at random with replacement.
- Sort the set R.
- Let d be the (
)th smallest element in the sorted set R.
- Let u be the (
)th smallest element in the sorted set R.
- By comparing every element in S to d and u< compute the set
and the numbers
and
.
- If
then FAIL.
- If
then sort the set C, otherwise FAIL.
- Output the (
)th element in the sorted order of C.