我的快速排序算法代码基于https://en.wikipedia.org/wiki/Quicksort:
def partition(L, left, right):
pivotIndex = left
pivotValue = L[left]
for i in range(left,right+1):
if L[i] < pivotValue:
L.insert(0, L.pop(i))
pivotIndex += 1
return pivotIndex
def _quicksort(data,left,right):
if left < right:
p = partition(data, left, right)
_quicksort(data, left, p-1) # sorting of the left side of the subarrays
_quicksort(data, p+1, right) # sorting of the right side of the subarrays
def quicksort(data):
L = list(data)
left = 0
right = len(L)-1
return _quicksort(L, left, right)
if __name__ == "__main__":
L = [3,4,5,2,3,5,5,2,1,7,2]
print quicksort(L)
它似乎适用于第一轮partition(),但是当左侧被分类时,它会更改pivotIndex,因此它以错误的索引开始,用于对右子阵列进行排序。有关如何制作此代码的任何建议吗?