-1
这是我的快速排序代码,partition
函数运行良好,但我在调用递归时遇到了问题。每次启动该功能时,pos
都会更改,然后列表限制也会改变。如何解决这个问题?快速排序python递归
def partition(lst, start, end):
pos=0
if len(lst)<2:
return
for i in range(len(lst[start:end])):
if lst[i] < lst[end]:
lst[i],lst[pos]=lst[pos],lst[i]
pos+=1
elif i==(len(lst[start:end])-1):
lst[end],lst[pos]=lst[pos],lst[end]
return pos
def quick_sort_recursive(lst, start, end):
pos=partition(lst, start, end)
if start<=pos<=end :
quick_sort_recursive(lst, start, pos-1)
quick_sort_recursive(lst, pos+1, end)
else:
return lst