Can anyone please help me in understanding the following python code

bhk755 at gmail.com bhk755 at gmail.com
Thu May 30 09:01:02 EDT 2013


Thanks Chris, Wolfgang and Joshua for your replies. 
------------------------

In step 2b, all the steps from 1 through 3 are executed again (twice). 

Soon, those calls will just output "Splitting" followed by "Merging"; 

and then we go back to 2c. That's why it *seems* that the code goes 

from 3 to 2c. You'll notice that the call depth decreases when this 

happens 

Chris, Can you please let me know what makes the control of the program code go to  2c after the output "Merging". 

Also, please look into the following output for the same program with more print statements, 
---------------------------------------------- 
 before calling main mergesort 
 Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] 
left half before split  [54, 26, 93, 17] 
right half before split [77, 31, 44, 55, 20] 
  Splitting [54, 26, 93, 17] 
left half before split  [54, 26] 
right half before split [93, 17] 
    Splitting [54, 26] 
left half before split  [54] 
right half before split [26] 
      Splitting [54] 
      Merging [54] 
      Splitting [26] 
      Merging [26] 
HERE AFTER SPLIT 
left half after split [54] 
right half after split [26] 
    Merging [26, 54] 
    Splitting [93, 17] 
left half before split  [93] 
right half before split [17] 
      Splitting [93] 
      Merging [93] 
      Splitting [17] 
      Merging [17] 
HERE AFTER SPLIT 
left half after split [93] 
right half after split [17] 
    Merging [17, 93] 
HERE AFTER SPLIT 
left half after split [26, 54] 
right half after split [17, 93] 
  Merging [17, 26, 54, 93] 
  Splitting [77, 31, 44, 55, 20] 
left half before split  [77, 31] 
right half before split [44, 55, 20] 
    Splitting [77, 31] 
left half before split  [77] 
right half before split [31] 
      Splitting [77] 
      Merging [77] 
      Splitting [31] 
      Merging [31] 
HERE AFTER SPLIT 
left half after split [77] 
right half after split [31] 
    Merging [31, 77] 
    Splitting [44, 55, 20] 
ft half before split  [44] 
ght half before split [55, 20] 
     Splitting [44] 
      Merging [44] 
      Splitting [55, 20] 
eft half before split  [55] 
ight half before split [20] 
       Splitting [55] 
       Merging [55] 
       Splitting [20] 
       Merging [20] 
HERE AFTER SPLIT 
left half after split [55] 
right half after split [20] 
      Merging [20, 55] 
HERE AFTER SPLIT 
left half after split [44] 
right half after split [20, 55] 
    Merging [20, 44, 55] 
HERE AFTER SPLIT 
left half after split [31, 77] 
right half after split [20, 44, 55] 
Merging [20, 31, 44, 55, 77] 
HERE AFTER SPLIT 
left half after split [17, 26, 54, 93] 
right half after split [20, 31, 44, 55, 77] 
 Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] 
 after calling main mergesort 
[17, 20, 26, 31, 44, 54, 55, 77, 93] 
------------------------------------------------------- 
   
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. 



More information about the Python-list mailing list