Reputation: 11
I tried to implement insertion sort in Python:
def insert_sort(list):
y= len(list)
for b in range(1,y):
value = list[b]
a = b-1
while a>= 0:
if list[a] > value:
list[a+1] = list[a]
list[a] = value
a = a -1
else:
break
li = [34, 55, 4, 455, 556, 33, 2, 54, 6, 8, 1]
l= insert_sort(li)
print (l)
But I can't tell if it even works because l
always comes out None
. What's wrong?
Upvotes: 1
Views: 181
Reputation: 14313
You're missing a return
statement:
def insert_sort(list):
y= len(list)
for b in range(1,y):
value = list[b]
a = b-1
while a>= 0:
if list[a] > value:
list[a+1] = list[a]
list[a] = value
a = a -1
else:
break
return list
li = [34, 55, 4, 455, 556, 33, 2, 54, 6, 8, 1]
l= insert_sort(li)
print (l)
Upvotes: 2