recursion - Selection Sort Recursive Python -
i have python code, recursive form of selection sort algorithm, doesn't work, can me?
def selection(lista): return selection_aux(lista,0,len(lista)) def selection_aux(lista,i,n): if == n or lista[0]==none: return lista else: num_menor = menor_f(lista,i,n,i) temporal = lista[i] lista[i] = num_menor print(num_menor) lista[num_menor] = temporal return selection_aux(lista,i+1,n) def menor_f(lista,j,n,menor): if j == n: return menor if lista[j] < lista[menor]: menor = j return menor_f(lista,j+1,n,menor) test:
>>> selection([1,2,3]) output
lista[num_menor] = temporal typeerror: list indices must integers, not nonetype
your indentation wrong, did not return if lista[j] < lista[menor] false setting num_menor none python functions don't return value return none default .
def menor_f(lista,j,n,menor): if j == n: return menor if lista[j] < lista[menor]: menor = j return menor_f(lista,j+1,n,menor) # dedent cannot follow variable names don't speak seems french, making change works selection([4,3,2])
print(selection([4,3,2])) [2, 3, 4] fails selection([ 8,3,4,2,5,6]) -> [3, 3, 4, 4, 5, 8] need debugging.
Comments
Post a Comment