Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'ideally': 0.04; 'output': 0.05; 'explicit': 0.07; 'made.': 0.07; 'nested': 0.07; 'subject:code': 0.07; 'subject:help': 0.08; 'check,': 0.09; 'function,': 0.09; 'output,': 0.09; 'returns,': 0.09; 'tackle': 0.09; 'python': 0.11; 'def': 0.12; '"here': 0.16; 'definition.': 0.16; 'does,': 0.16; 'grasp': 0.16; 'itself,': 0.16; 'nest': 0.16; 'nesting': 0.16; 'recurse': 0.16; 'reversed': 0.16; 'silly': 0.16; 'subject: \n ': 0.16; 'subject:skip:u 10': 0.16; 'writing).': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'library': 0.18; 'trying': 0.19; 'work,': 0.20; 'written': 0.21; 'example': 0.22; 'handles': 0.22; 'header:User-Agent:1': 0.23; 'passes': 0.24; 'simpler': 0.24; 'url:moin': 0.24; 'sort': 0.25; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'chris': 0.29; 'am,': 0.29; 'statement': 0.30; 'subject:please': 0.30; 'url:wiki': 0.31; 'directly,': 0.31; 'know.': 0.32; 'another': 0.32; 'url:python': 0.33; 'beginning': 0.33; 'subject:the': 0.34; 'could': 0.34; 'problem': 0.35; 'problem.': 0.35; 'something': 0.35; 'case,': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'instances': 0.36; 'done': 0.36; 'doing': 0.36; 'url:org': 0.36; 'should': 0.36; 'wrong': 0.37; 'turn': 0.37; 'list': 0.37; 'clear': 0.37; 'to:addr:python-list': 0.38; 'that,': 0.38; 'does': 0.39; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'called': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'eventually': 0.60; 'middle': 0.60; 'solve': 0.60; 'subject:Can': 0.60; 'most': 0.60; 'tell': 0.60; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'email addr:gmail.com': 0.63; 'line,': 0.68; 'past,': 0.68; 'received:74.208': 0.68; 'statement,': 0.68; 'useful.': 0.68; 'study': 0.69; 'received:74.208.4.194': 0.84; 'recursion:': 0.84; 'continue.': 0.91; 'reply,': 0.93 Date: Thu, 30 May 2013 09:29:52 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Can anyone please help me in understanding the following python code References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> <384a9b8f-40e0-4c8c-b15f-0c96512f67f1@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:2L3oX54Eg26rm7xJxLd9R8fHmnmBeQ4RiinpUeVpYV7 5qAo2I0VSk1GtF+gDFrptPG6r54FcM4XYwIu86fYlFyXLMj0YN PRjLNMAUkJjx63V47mfikKsgZfDHpU9R/5B7BAgxKwjbKdAzdq fSVwBxD66xpcBuENP1/m0ZPBq45ENFQyF5eyAdLNFPe9CsyzkW Gc5Uvz7URYGFdbYdApTyP1RuykzkrroVn5CWQQC/8wMxXqesso xYygDcQjXO65Mcfz0UagbBQgHQZD7+gHJq98Z2GNMfdjRC2y7E HR6bwGNaYaSITVUgSwxzlwEsN71z+xZ/FglxsbE/r03MbH1ZBm cKsZN4Z2mjs5O0OcKmz8= 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: 104 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369920608 news.xs4all.nl 15888 [2001:888:2000:d::a6]:34876 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46490 On 05/30/2013 08:42 AM, bhk755@gmail.com wrote: >> >> In the above output, the control goes to "HERE AFTER SPLIT" after the "Merging" statement which is of-course the last statement in the function.On what condition this is happening. >> >> Ideally as per my understanding, after the last statement of a function the control should come out of the function. >> >> Please correct me if I am wrong here. >> >> There is something with respect-to functions in python that I am not able to understand. > I think you misunderstand recursion. And you should study it in a simpler example before trying to tackle that sort function. After the last statement of a function, or after an explicit return statement, the function does indeed return control to whoever called it. And if the call was in the middle of some other function, that's the place where execution will continue. Functions may nest this way to pretty large depths of complexity. If you're not sure you understand that, we should stop here. So in your reply, tell me if you can readily grasp function dfunc() calling cfunc(), which calls bfunc(), which calls afunc(). When afunc() returns, you'll be right in the middle of bfunc(), right after the call was made. Now to recursion. One can write a function which solves a particular problem by doing some of the work, but by calling on other functions which solve simpler subproblems. Recursion comes in when those other functions are simply instances of the same one. So instead of dfunc() calling cfunc(), we have rfunc() calling rfunc() calling rfunc() calling rfunc(). Let's take a classic problem for explaining recursion: factorial. We can write a simple function that solves the problem for 0: def afunc(n): if n == 0: return 1 else: throw-some-exception Silly function, I know. Now let's write one that solves it for one. def bfunc(n): if n == 1: return 1 * afunc(n-1) else: throw-some-exception Now for two: def bfunc(n): if n == 2: return 2 * bfunc(n-1) else: throw-some-exception Notice that each function calls the one above it to solve the "simpler" problem. Now if we wanted this to work for n=100, it would get really tedious. So let's see if we can write one function that handles all of these cases. def rfunc(n): if n == 0: return 1 else: return n * rfunc(n-1) Now if we call this with n==3, it'll make the zero check, then it'll call another function with the value 2. that one will in turn call another function with the value 1. And so on. So this function calls itself, as we say recursively. Now, for this simple case, we could have used a simple loop, or just called the library function. But it's a simple enough example to follow in its entirety (if I've been clear in my writing). Your mergeSort() function calls itself recursively, and each time it does, it passes a *smaller* list to be sorted. Eventually the calls recurse down to a list of size 1, which is already sorted by definition. The check for that is the line if len(alist)>1: near the beginning of the function. That's analogous to my if n==0 line, although to match it most directly, I could have reversed the if/else and written the test as if n!=0 Chris showed you how to change your output so you could see the nesting levels. I have done that sort of thing in the past, and found it very useful. But first you have to understand nested functions, and simple recursion. -- DaveA