Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.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.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:code': 0.07; 'subject:help': 0.08; 'executed': 0.09; 'seemed': 0.09; 'stack.': 0.09; 'python': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guarded': 0.16; 'indent': 0.16; 'parameter.': 0.16; 'subject:skip:u 10': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'starts': 0.20; 'print': 0.22; 'skip:% 10': 0.24; 'this:': 0.26; 'skip:" 20': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'rest': 0.29; 'statement': 0.30; 'subject:please': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; 'continues': 0.31; 'depth': 0.31; 'once,': 0.31; 'prints': 0.31; 'compatible': 0.32; 'says': 0.33; 'style': 0.33; 'subject:the': 0.34; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'called': 0.40; 'according': 0.40; 'subject:Can': 0.60; 'places': 0.64; 'taking': 0.65; '30,': 0.65; 'clearer': 0.84; 'how.': 0.84; 'off,': 0.84; 'same,': 0.91; '2013': 0.98 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 :content-type; bh=RtElLj6/iXkvMrVzboBOPXS+tajfIhg3QUlhjWDl12M=; b=bBlSpTkYvX6MQz5DCYCiAkqaxRnqZS61GbHe5yicTT2fWv967Bspx2EsQ0artec1V8 NCIoel2tYigJgCob2oaK/VS9ueHERgCSSbk37djlcKFyatnIS/ARRov3hCl7D3ipQBR1 /GWsxPk6R8hB9P3D/Y4KcoZoBKuTzih0z2zA4vTS3LQKfTiDBbfqTiRElVJHJewRC8Wf 55YAoC9wgcoUTy5Yt9DELaroJt4hQszYbSp+auRXmhTTokMq3b0pQO051ZroDrJMIokp Ert+MnRKSV0gIyLHcZhOnZthyptJdhsjYyQ9izIFdr3DDWfMC34CtgbrcmjFg6jf8u6b wzAQ== MIME-Version: 1.0 X-Received: by 10.52.121.99 with SMTP id lj3mr3990929vdb.52.1369908079684; Thu, 30 May 2013 03:01:19 -0700 (PDT) In-Reply-To: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Date: Thu, 30 May 2013 20:01:19 +1000 Subject: Re: Can anyone please help me in understanding the following python code 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369908087 news.xs4all.nl 16009 [2001:888:2000:d::a6]:34835 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46448 On Thu, May 30, 2013 at 7:48 PM, wrote: > Function mergeSort is called only once, but it is getting recursively executed after the printing the last statement "print("Merging ",alist)". But don't recursion taking place except at these places "mergeSort(lefthalf), mergeSort(righthalf)" > > Sometimes the function execution directly starts from i=0,j=0,k=0 . Not sure how. When it says "Splitting" with a single-element list, it then immediately prints "Merging" and returns (because all the rest of the code is guarded by the 'if'). Execution then continues where it left off, in the parent. One good way to get an idea of what's going on is to add a recursion depth parameter. Keep all the rest of the code the same, but change the print and recursion lines to be like this: def mergeSort(alist,depth): print("%*cSplitting %r"%(depth*2,' ',alist)) # ... mergeSort(lefthalf,depth+1) mergeSort(righthalf,depth+1) # ... print("%*cMerging %r"%(depth*2,' ',alist)) mergeSort(alist,0) That'll indent everything according to the depth of the call stack. (Also, I changed your 'print's to be compatible with Python 2 and Python 3; you seemed to have Python 3 style function calls and a Python 2 interpreter.) Hopefully that'll make clearer what's going on! ChrisA