我正在尝试在 python 中编写所有排序算法的迭代和递归版本。除了我没有返回任何东西之外,这有什么问题?我的切片有问题吗?
尝试解决方案:
def insertOne(element, aList):
''' Inserts element into its proper place in a sorted list aList.
input: element is an item to be inserted. aList is a sorted list.
output: A sorted list.
'''
if len(aList) == 0:
return [element]
elif element < aList[0]:
return [element] + aList
else:
return aList[0:1] + insertOne(element, aList[1:])
def sort(aList):
if len(aList) == 0:
return []
n = len(aList)
if n > 1:
sort(aList[:n - 1])
insertOne(n, aList)
aList = [3,2,1]
print sort(aList)