efficient partial sort in Python ?

Ian Kelly ian.g.kelly at gmail.com
Tue Aug 19 20:00:58 EDT 2014


On Tue, Aug 19, 2014 at 5:05 PM, Dan Stromberg <drsalists at gmail.com> wrote:
> When you use heapq, are you putting all the values in the heap, or
> just up to n at a time (evicting the worst value, one at a time as you
> go)?  If you're doing the former, it's basically a heapsort which
> probably won't beat timsort.  If you're doing the latter, that should
> be pretty good.

The latter is what the heapq.nsmallest and heap.nlargest functions do.
nsmallest uses a max heap, so the value being evicted is still found
at index 0.

Curious then if it's performing worse memory-wise than sorted.



More information about the Python-list mailing list