Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.092 X-Spam-Evidence: '*H*': 0.82; '*S*': 0.00; 'algorithm': 0.04; 'iterate': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '(it': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'mylist': 0.16; 'subject:Sort': 0.16; 'elements': 0.16; 'index': 0.16; 'wrote:': 0.18; 'feb': 0.22; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'order.': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'subject:) ': 0.29; 'involving': 0.30; 'message- id:@mail.gmail.com': 0.30; '(which': 0.31; 'subject:time': 0.33; 'sense': 0.34; "can't": 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'subject:one': 0.36; 'doing': 0.36; 'list': 0.37; 'problems': 0.38; 'itself': 0.39; 'space': 0.40; 'even': 0.60; 'read': 0.60; 'algorithms': 0.60; 'new': 0.61; 'simply': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'hey,': 0.75; 'contains.': 0.84; 'more:': 0.84; 'oscar': 0.84; 'subject:space': 0.84; 'technically': 0.84; 'absolutely': 0.87 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 :cc:content-type; bh=7+4unq8J3TewfHykxX3oPpgLU9hzvbOAfv9BfRfMT/4=; b=g1x8YWwshlqwqPkBb9QHB2hrZpqYNKd5K0zIVBjzD9fDImkWmMhpRyLwr1oEge9/5m gDfhguLTty9cQz5zuqFcCNByYzbjFLTJJ4/xiVZUNXTjHY8WoW+uNmzAYvNa9yY0uETL uCSk50/YE3ZMvodYBv7JBEVQxaHmm8879dXmYSoVoNDQGkSX1rt/an888iiHD0bbk/RC o6tVzBPQ13njwkbO/EKLEReyLIuOxnb+ghv2TZRbtAwjA6r2Tj3wZJaouS9ATJRvDzOT jaTOCrsj+aQ+0lOVatr03DTDBfWGPaVxmBb6teK8PMOLbhWUZj8Ank7wkGiN0GuPNAdN QFLA== X-Received: by 10.58.168.142 with SMTP id zw14mr777154veb.33.1392050513513; Mon, 10 Feb 2014 08:41:53 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <52f80bca$0$29972$c3e8da3$5496439d@news.astraweb.com> <9671c418-cde8-4857-85ec-52380f8390eb@googlegroups.com> <433934582413720306.205290sturla.molden-gmail.com@news.gmane.org> <2079199387413737237.368296sturla.molden-gmail.com@news.gmane.org> From: Oscar Benjamin Date: Mon, 10 Feb 2014 16:41:33 +0000 Subject: Re: Sort one sequence by O(n) in time and O(1) in space To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392050523 news.xs4all.nl 2937 [2001:888:2000:d::a6]:55919 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:65831 On 10 February 2014 15:52, Chris Angelico wrote: > On Tue, Feb 11, 2014 at 2:45 AM, Oscar Benjamin > wrote: >> Something like >> >> mylist[:] = reorder_generator(mylist) >> >> won't work because the generator would need to access the data >> non-sequentially (it would need to read elements after they were >> overwritten). > > This would have O(1) space and O(n) time. It's absolutely perfect... > except that you now don't technically have a list any more: > > mylist = reorder_generator(mylist) > > You can iterate over it, but can't index it. But hey, it complies with > the space/time requirements! That is very likely a practical solution for many problems involving transposition. Doing this in Python is pretty artificial since a new list object will likely use a lot less memory than the elements it contains. The practical use for such algorithms is in something like C and even then it's often better to simply iterate in a different order. The advantage of physically transposing the data is to improve memory locality but this only makes sense if your transpose algorithm itself has a good memory access pattern (which is likely impossible with O(1) storage). Oscar