Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76501
| References | <51dfbe9b-f6e0-4532-bc2d-e7ce2fc282b5@googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2014-08-18 12:48 -0600 |
| Subject | Re: efficient partial sort in Python ? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13109.1408387733.18130.python-list@python.org> (permalink) |
On Mon, Aug 18, 2014 at 11:18 AM, Chiu Hsiang Hsu <wdv4758h@gmail.com> wrote: > I know that Python use Timsort as default sorting algorithm and it is efficient, > but I just wanna have a partial sorting (n-largest/smallest elements). > > In the current state, I can only find this kind of functions from heapq, > but it's too slow for this problem, > the pure c sorted function can easily beat heapq.nlargest even though it sort all the elements. > > I think it would be better if we have an effeicient partial sorting function, > maybe it could be like this : partial_sorted(data, n), data.partial_sort(n) > > Does anyone have any more information for partial sorting ? > > Btw, C++ has a partial_sort function in <algorithm>, but I don't know what algorithm it really use. According to http://www.sgi.com/tech/stl/partial_sort.html the STL implementation also uses heapsort. For better partial sorting algorithms, try Wikipedia: http://en.wikipedia.org/wiki/Partial_sorting However, I think you're still not likely to beat Timsort for smallish data sets unless you also implement it in C.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
efficient partial sort in Python ? Chiu Hsiang Hsu <wdv4758h@gmail.com> - 2014-08-18 10:18 -0700
Re: efficient partial sort in Python ? Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-18 12:48 -0600
Re: efficient partial sort in Python ? Dan Stromberg <drsalists@gmail.com> - 2014-08-18 14:42 -0700
Re: efficient partial sort in Python ? Chiu Hsiang Hsu <wdv4758h@gmail.com> - 2014-08-19 12:37 -0700
Re: efficient partial sort in Python ? Terry Reedy <tjreedy@udel.edu> - 2014-08-19 18:11 -0400
Re: efficient partial sort in Python ? Dan Stromberg <drsalists@gmail.com> - 2014-08-19 16:05 -0700
Re: efficient partial sort in Python ? Dan Stromberg <drsalists@gmail.com> - 2014-08-19 16:10 -0700
Re: efficient partial sort in Python ? Dan Stromberg <drsalists@gmail.com> - 2014-08-19 16:22 -0700
Re: efficient partial sort in Python ? Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-19 18:00 -0600
csiph-web