我在Brad Miller和David Ranum(http://interactivepython.org/runestone/static/pythonds/SortSearch/sorting.html#the-quick-sort)中偶然发现了#34;使用算法和数据结构解决问题"中的快速排序算法。
在此处显示的快速排序算法将列表中的第一个值作为透视值。练习是修改程序以选择枢轴值作为三个中值。这是原始剧本:
def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)
def quickSortHelper(alist,first,last):
if first<last:
splitpoint = partition(alist,first,last)
quickSortHelper(alist,first,splitpoint-1)
quickSortHelper(alist,splitpoint+1,last)
def partition(alist,first,last):
pivotvalue = alist[first]
leftmark = first+1
rightmark = last
done = False
while not done:
while leftmark <= rightmark and \
alist[leftmark] <= pivotvalue:
leftmark = leftmark + 1
while alist[rightmark] >= pivotvalue and \
rightmark >= leftmark:
rightmark = rightmark -1
if rightmark < leftmark:
done = True
else:
temp = alist[leftmark]
alist[leftmark] = alist[rightmark]
alist[rightmark] = temp
temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp
return rightmark
我修改了一下,首先添加了median()
函数:
def median(data):
sd = sorted(data)
N = len(data) - 1
a = sd[N // 2]
b = sd[(N + 1) // 2]
return (a+b) // 2
然后,在partition()
函数中,将pivotvalue
修改为:
pivotvalue = median([alist[0]] + [alist[len(alist)-1]] + [alist[len(alist)//2]])
并将leftmark
更改为以索引0
开头,而不是1
:
leftmark = first
而不是:
leftmark = first+1
然后更改了最后done == True
时要执行的步骤,以便正确地交换rightmark
和pivot
值:
temp = pivotvalue
alist[alist.index(pivotvalue)] = alist[rightmark]
alist[rightmark] = temp
但是当用:
调用时alist = [77,26,93,17,54,31,44,55,20]
quickSort(alist)
print(alist)
我得到了:
Traceback (most recent call last):
File "/home/reloader/Templates/Exercises/quick_sort.py", line 52, in <module>
quickSort(alist)
File "/home/reloader/Templates/Exercises/quick_sort.py", line 9, in quickSort
quickSortHelper(alist,0,len(alist)-1)
File "/home/reloader/Templates/Exercises/quick_sort.py", line 17, in quickSortHelper
quickSortHelper(alist,splitpoint+1,last)
File "/home/reloader/Templates/Exercises/quick_sort.py", line 17, in quickSortHelper
quickSortHelper(alist,splitpoint+1,last)
File "/home/reloader/Templates/Exercises/quick_sort.py", line 17, in quickSortHelper
quickSortHelper(alist,splitpoint+1,last)
File "/home/reloader/Templates/Exercises/quick_sort.py", line 17, in quickSortHelper
quickSortHelper(alist,splitpoint+1,last)
File "/home/reloader/Templates/Exercises/quick_sort.py", line 17, in quickSortHelper
quickSortHelper(alist,splitpoint+1,last)
File "/home/reloader/Templates/Exercises/quick_sort.py", line 17, in quickSortHelper
.......
RuntimeError: maximum recursion depth exceeded in comparison
由于我发现这个算法有点复杂(并且执行的步骤太多),我真的不知道要改变什么才能使其工作,而我所做的就是打破了修改。我只是将数据透视值设置为列表中间的值。是否还应修改我目前看不到的其他内容?
感谢。
修改
如果我将leftmark
的初始值保留为firstmark + 1
(即列表的索引1),我没有得到无限递归错误,但是列表也没有正确排序:
[55, 26, 31, 44, 17, 77, 54, 20, 93]
答案 0 :(得分:2)
您必须从被分区的子阵列中选择中位数:
替换它:
pivotvalue = alist[first]
与
pivotindex = median(alist, first, last, (first + last) // 2)
alist[first], alist[pivotindex] = alist[pivotindex], alist[first]
pivotvalue = alist[first]
并且中位数查找器不必如此复杂。
def median(a, i, j, k):
if a[i] < a[j]:
return i if a[k] < a[i] else k if a[k] < a[j] else j
else:
return j if a[k] < a[j] else k if a[k] < a[i] else i