Why I can't return function value? Return gives "none"?

Question:

def sel(unsort,current):
    if len(unsort) == 0:
        return current

    temp = unsort[0]    
    for i in range(len(unsort)):
        if unsort[i]<temp:
            temp = unsort[i];

    unsort.remove(temp)
    current = current + [temp];

sel(unsort,current)

Above will define a function selection sort.

a = [4,3,2,1];
print(sel(a, []))

When I run the program on python, it print “None”. The function should return current as a list. What have I done wrong here?

Asked By: user45765

||

Answers:

When you recursively call sel, you ignore the return value.

Change it to return:

    return sel(unsort, current)
Answered By: Jonathon Reinhart
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.