2

I’m trying to get a better understanding of recursion using the merge sort algorithm using Python 2.7. I have written a small snippet of code to break down a list recursively. The code seems to work fine except for the last step. For the base case, the program is supposed to return a list of size 1. However, it is returning the value “none”. Where am I going wrong?

mylist = [14,88,2,14,9,123,1,5]
def concour(thelist):
    mid = len(thelist) / 2
    LeftSide = thelist[:mid]
    print LeftSide,'length is ', len(LeftSide)
    if len(LeftSide) == 1:          #the base case here
        print LeftSide        
        return LeftSide
    else:
        concour(LeftSide)    #recursive call

print concour(mylist)



"""
[14, 88, 2, 14] length is  4
[14, 88] length is  2
[14] length is  1
[14]
None
"""
1
  • Please accept this answer below. And if possible can you include at the end of the thread "is returning None" Commented Sep 17, 2019 at 10:37

1 Answer 1

2

You're missing your return statement in the recursive call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.