Below are definitions TLA+ operators that convert a set of integers into a sorted sequence. You can take the first N elements of SortedSeqFromSet(Range(f)) to retrieve the N smallest values of the function f. Evaluation of these operators still requires quadratic time, and although you could write a Quicksort operator in TLA+, if you really care about efficiency you should probably consider overriding the definition of SortedSeqFromSet(_) by a Java method.
RECURSIVE InsertSorted(_,_,_), SortedSeqFromSet(_)
InsertSorted(x, seq, i) == \* insert element in a sorted sequence of integers, starting from index i
IF i > Len(seq) THEN seq \o << x >>
ELSE IF x <= seq[i] THEN SubSeq(seq, 1, i-1) \o << x >> \o SubSeq(seq, i, Len(seq))
ELSE InsertSorted(x, seq, i+1)
SortedSeqFromSet(S) == \* convert a set of integers into a sorted sequence
IF S = {} THEN << >>
ELSE LET x == CHOOSE x \in S : TRUE
rest == SortedSeqFromSet(S \ {x})
IN InsertSorted(x, rest, 1)