Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'algorithm': 0.04; 'subject:Python': 0.06; 'algorithms,': 0.07; 'elements.': 0.07; 'function,': 0.09; 'stl': 0.09; 'python': 0.11; 'elements).': 0.16; 'sorting': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'aug': 0.22; 'mon,': 0.24; 'sort': 0.25; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'sets': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:wiki': 0.31; 'url:wikipedia': 0.31; 'anyone': 0.31; 'maybe': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'c++': 0.36; 'url:org': 0.36; 'too': 0.37; 'easily': 0.37; 'implement': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'use.': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'according': 0.40; 'even': 0.60; "you're": 0.61; 'information': 0.63; 'kind': 0.63; 'more': 0.64; 'beat': 0.68; 'default': 0.69; 'partial': 0.84; 'url:tech': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=akFsSG2Kb69Lt3S+FSEr6bqdWNKcARJK8nxJ6FFxJBI=; b=Wy+eiPo+uohrR0mHqdc5cGCHCgn3JffnpaJ43zUWad2JQLEhwtmP8bKqgG6Nat+myx M0nwzpnQg+uadciSNkptsRce58JjnuN3rWE9xiVxP+k2GN8k+j7UFIGjNBjECYWWZeyj c21uO1cn64vCBnqUGq7TKkpzw2ObO3e4m3lFzy7nbAb28V+JO49QV+6tV8CQ7htVeL19 J6R60G7AuMfoPvmN17bEgfVak7tzh9KRiU3kUzqynSM8jcFjgXO/EpbiGsqIj62XeiOe AydSWXwI6W1xJLALgfbuUpAV+rJMOyfo542zUwDTUB7VqHCmr/FsmvQn1W1AgOl20/TY /yXg== X-Received: by 10.66.243.6 with SMTP id wu6mr6921785pac.157.1408387724188; Mon, 18 Aug 2014 11:48:44 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <51dfbe9b-f6e0-4532-bc2d-e7ce2fc282b5@googlegroups.com> References: <51dfbe9b-f6e0-4532-bc2d-e7ce2fc282b5@googlegroups.com> From: Ian Kelly Date: Mon, 18 Aug 2014 12:48:04 -0600 Subject: Re: efficient partial sort in Python ? To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408387733 news.xs4all.nl 2950 [2001:888:2000:d::a6]:49441 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76501 On Mon, Aug 18, 2014 at 11:18 AM, Chiu Hsiang Hsu 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 , 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.