Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4a.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.057 X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'list?': 0.07; 'okay': 0.09; 'cc:addr:python-list': 0.11; '>>': 0.16; "(it's": 0.16; 'algorithm.': 0.16; 'cc:name:python list': 0.16; 'guys,': 0.16; 'in-place': 0.16; 'pair.': 0.16; 'subject:Sort': 0.16; 'swaps': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'help.': 0.21; 'input': 0.22; 'example': 0.22; 'email addr:gmail.com>': 0.22; 'otherwise,': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'looks': 0.24; 'question': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'subject:) ': 0.29; 'related': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:wiki': 0.31; 'bunch': 0.31; 'url:wikipedia': 0.31; 'subject:time': 0.33; 'could': 0.34; 'problem': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'subject:one': 0.36; 'done': 0.36; 'doing': 0.36; 'url:org': 0.36; 'so,': 0.37; 'list': 0.37; 'list,': 0.38; 'rather': 0.38; 'skip:& 20': 0.39; 'space': 0.40; 'how': 0.40; 'algorithms': 0.60; 'new': 0.61; "you're": 0.61; 'first': 0.61; 'here:': 0.62; 'offer': 0.62; 'information': 0.63; 'to:addr:gmail.com': 0.65; 'details': 0.65; 'here': 0.66; 'reply': 0.66; 'yes': 0.68; 'paper': 0.75; 'charset:gb2312': 0.80; 'complexity': 0.84; 'oscar': 0.84; 'subject:space': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=D4o4bIIgC31eCVjiAyeB0rX72qJcLC9AG2sbTBvPK6U=; b=y02bDPayMlRk81u2nnsw5Ffykqi51turPmt1ELBPZEmfCyTptUl2t3dlEkRwOMMFzw bPDLGKiD3iENWBT65GtG+OeJBZJ/XrTpdlOu4oDOS4zDSr6I4N0iEnf1PXL9bnPy8XZb 1Cjf8u/a2CMK3XJ8w14ElPV0u7chMRn4pSsjy3ZkKGiA7r2aWuD2ox30c0ykdKvhgvWN MC4NgA+jzVAEXg7kYY897wHSf4kP3V7lSoYa6sd1l6uoN4Hh+dcANwCMwm1bzZq2qycM JimUegXcTvhETpbRPl0JmYuTvUF/UGfSXyeK9T3E7N2XEPyIKR/V5wrIJJ0VTeIwH1m6 ybow== MIME-Version: 1.0 X-Received: by 10.58.132.203 with SMTP id ow11mr19783847veb.1.1391956838975; Sun, 09 Feb 2014 06:40:38 -0800 (PST) In-Reply-To: References: Date: Sun, 9 Feb 2014 22:40:38 +0800 Subject: Re: Sort one sequence by O(n) in time and O(1) in space From: Ni Wesley To: Oscar Benjamin Content-Type: multipart/alternative; boundary=047d7b66f00fee0cec04f1fa34b3 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: 102 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391956847 news.xs4all.nl 2912 [2001:888:2000:d::a6]:49380 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65762 --047d7b66f00fee0cec04f1fa34b3 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: quoted-printable Yes, with no new list, otherwise, space won't to be O(1) Wesley 2014=C4=EA2=D4=C29=C8=D5 =CF=C2=CE=E710:31=D3=DA "Oscar Benjamin" =D0=B4=B5=C0=A3=BA > Please reply to the list rather than directly to me so that other > people can see the answer to my question and offer you help. > > On 9 February 2014 14:04, Ni Wesley wrote: > > 2014=C4=EA2=D4=C29=C8=D5 =CF=C2=CE=E79:41=D3=DA "Oscar Benjamin" =D0=B4=B5=C0=A3=BA > > > >> On 9 February 2014 12:13, Wesley wrote: > >> > Hi guys, > >> > Here is one question related to algorithm. > >> > Details here: > >> > > >> > here is input sequence like a1,a2,...,an,b1,b2,...,bn =A3=ACthe ax a= nd bx > >> > always exist in pair. So, now, how to change the sequence to > >> > a1,b1,...,an,bn, with time complexity as O(n) and space as O(1). > >> > >> Do you mean that you have a list and you want to rearrange the > >> elements in-place without creating a new list? > > > > Yes > > Okay so if you're going to do it with O(1) space then it's going to > have to be done with a whole bunch of swaps. Have a think with pen and > paper about how you could do a sequence of swaps that would rearrange > the order to the one that you want (it's actually a harder problem > than it looks at first glance). This is an example of what is known as > "transposition" and much information is available about algorithms for > doing this in-place: > http://en.wikipedia.org/wiki/In-place_matrix_transposition > > > Oscar > --047d7b66f00fee0cec04f1fa34b3 Content-Type: text/html; charset=GB2312 Content-Transfer-Encoding: quoted-printable

Yes, with no new list, otherwise, space won't to be O(1)=

Wesley

2014=C4=EA2=D4=C29=C8=D5 =CF=C2=CE=E710:31=D3=DA= "Oscar Benjamin" <oscar.j.benjamin@gmail.com>=D0=B4=B5=C0=A3=BA
Please reply to the list rather than directly to me so that other
people can see the answer to my question and offer you help.

On 9 February 2014 14:04, Ni Wesley <nispray@gmail.com> wrote:
> 2014=C4=EA2=D4=C29=C8=D5 =CF=C2=CE=E79:41=D3=DA "Oscar Benjamin&q= uot; <oscar.j.benjamin@gma= il.com>=D0=B4=B5=C0=A3=BA
>
>> On 9 February 2014 12:13, Wesley <nispray@gmail.com> wrote:
>> > Hi guys,
>> >    Here is one question related to algorithm.
>> > Details here:
>> >
>> > here is input sequence like a1,a2,...,an,b1,b2,...,bn =A3=ACt= he ax and bx
>> > always exist in pair. So, now, how to change the sequence to<= br> >> > a1,b1,...,an,bn, with time complexity as O(n) and space as O(= 1).
>>
>> Do you mean that you have a list and you want to rearrange the
>> elements in-place without creating a new list?
>
> Yes

Okay so if you're going to do it with O(1) space then it's going to=
have to be done with a whole bunch of swaps. Have a think with pen and
paper about how you could do a sequence of swaps that would rearrange
the order to the one that you want (it's actually a harder problem
than it looks at first glance). This is an example of what is known as
"transposition" and much information is available about algorithm= s for
doing this in-place:
http://en.wikipedia.org/wiki/In-place_matrix_transposition


Oscar
--047d7b66f00fee0cec04f1fa34b3--