我试图执行this video中讨论的算法,并且还在this document中。使用快速排序(Python)查找第k个最小项目
我的快速排序的代码,这取决于拾取中间元件阵列在作为枢轴(见下文),而不是由上面的文档的作者,它使用第一元件在使用的方法作为shown here in the video的数据集。
显然,我的代码不起作用(最终用完了递归限制)。我想知道是否是因为我的代码中存在一些愚蠢的错误,或者只要我从中间选择中心点,它就无法工作。
def partition(a, start, end):
# I pick the pivot as the middle item in the array
# but the algorithm shown in the video seems to
# pick the first element in the array as pivot
piv = (start + end) // 2
pivotVal = a[piv]
left = start
right = end
while left <= right:
while a[left] < pivotVal:
left += 1
while a[right] > pivotVal:
right -= 1
if left <= right:
temp = a[left]
a[left] = a[right]
a[right] = temp
left += 1
right -= 1
return left
def quicksort(a, start, end, k):
if start < end:
piv = partition(a, start, end)
if piv == (k - 1):
print("Found kth smallest: " + piv)
return a[piv]
elif piv > (k - 1):
return quicksort(a, start, piv, k)
else:
return quicksort(a, piv + 1, end, k)
myList = [54, 26, 93, 17, 77, 31, 44, 55, 20]
quicksort(myList, 0, len(myList) - 1, 3)
print(myList)
您是否检查过quickselect算法? –
您正在使用包含数组边界。你正在做这个不必要的努力。几乎所有算法都使用[start,end)更加优雅地描述。 – orlp
您在'partition'功能中混合使用'a'和'arr'。 – thefourtheye