What´s the most efficient, elegant and pythonic way of solving this problem?
Given a list (or set or whatever) of n elements, we want to get the k biggest ones. ( Yo
The simple, O(n log n) way is to sort the list then get the last k elements.
The proper way is to use a selection algorithm, which runs in O(n + k log k) time.
Also, heapq.nlargest takes O(k log n) time on average, which may or may not be good enough.
(If k = O(n), then all 3 algorithms have the same complexity (i.e. don't bother). If k = O(log n), then the selection algorithm as described in Wikipedia is O(n) and heapq.nlargest is O(n log log n), but double logarithm is "constant enough" for most practical n that it doesn't matter.)