Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'algorithm': 0.03; 'way:': 0.09; 'subject:python': 0.11; 'algorithm.': 0.16; 'benjamin': 0.16; 'bisect': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'require,': 0.16; 'sorting': 0.16; 'subject:3.3': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'comparing': 0.17; 'element': 0.17; 'suggested': 0.20; 'sort': 0.21; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'first,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'run': 0.28; 'faster,': 0.29; 'function': 0.30; 'running': 0.32; 'could': 0.32; 'zero': 0.33; 'to:addr:python-list': 0.33; 'know.': 0.33; 'another': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'minimum': 0.34; 'list': 0.35; 'faster': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'two': 0.37; 'item': 0.37; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'performance': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'your': 0.60; 'easy': 0.60; '2013': 0.84; 'oscar': 0.84; 'subject:Min': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=pjLqIT+NMHE5IA0vXl+A4zNJR05RcyBHbn+h86XjaGk=; b=y5pwqTgu36imTq/pbIyKxgrO+VHxFkTI+Jhdvwq9FXtFMnos2wJXnk+wanKaNzCEuJ 3JNyHAU2kS7xcES9pykh8D4qYlZQzFF08nbzuz0Kt2j0xzF6504suFm4SuPfJQC8Vgb3 ElxImuq4riLMGaPsbOoQ2X2ibEpGVFpA9sGeDqiZAxLepQmVN/y1QhQySsZgvRW4RbCx 4wQWJgHSGTtZtv6F+UUg7fLBvxyFVeYC+Ba16dbQSOVR32bdJ/UBVBlR3CFUgmUriBIm G+6Ixli5aFAJEME/+2P9q+zOaaVJr0gnj5vJJn8u+4NHhknyuJ+y7eEkgwmXcgWC06kD URiw== MIME-Version: 1.0 X-Received: by 10.52.88.197 with SMTP id bi5mr6917912vdb.58.1363174582119; Wed, 13 Mar 2013 04:36:22 -0700 (PDT) In-Reply-To: References: <513fdcfc$0$29965$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 13 Mar 2013 22:36:21 +1100 Subject: Re: Finding the Min for positive and negative in python 3.3 list From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363174584 news.xs4all.nl 6908 [2001:888:2000:d::a6]:53298 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41173 On Wed, Mar 13, 2013 at 10:23 PM, Oscar Benjamin wrote: > On 13 March 2013 10:43, Wolfgang Maier > wrote: >> >> thinking again about the question, then the min() solutions suggested so far >> certainly do the job and they are easy to understand. >> However, if you need to run the function repeatedly on larger lists, using min() >> is suboptimal because its performance is an O(n) one. >> It's faster, though less intuitive, to sort your list first, then use bisect on >> it to find the zero position in it. Two manipulations running at O(log(n)). > > Sort cannot be O(log(n)) and it cannot be faster than a standard O(n) > minimum finding algorithm. No valid sorting algorithm can have even a > best case performance that is better than O(n). This is because it > takes O(n) just to verify that a list is sorted. Or looking at it another way: Sorting a list will require, at a bare minimum, comparing every element against at least one other element - if you could reduce it below that, there would be some element whose position you cannot know. Finding the minimum requires precisely that number of comparisons: each item against the one current minimum. :) ChrisA