Can anyone please help me in understanding the following python code

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Thu May 30 06:17:11 EDT 2013


 <bhk755 <at> gmail.com> writes:
>
> 
> 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.

Maybe you should tell us first what output you would have expected.
It's not really clear from your question what confuses you. Doesn't the
algorithm do what it's supposed to do? When does the function start from
i=0, j=0, k=0 ? What exactly is the output then ?
Maybe you are confused by the fact that the algorithm is recursive twice. It
first goes through the lefthand branch, then only on its way out it works on
the righthand branch. So the recursion through mergeSort is not started only
once as you say, but twice (after finishing the recursion through
mergeSort(lefthand), another recursion is kicked off by calling
mergeSort(righthand).
Best,
Wolfgang





More information about the Python-list mailing list