我正在尝试在 Python 3 中的整数 ctypes 数组上实现几个排序方法,但似乎无法弄清楚 Quicksort 方法。我相信我的大部分代码都是正确的,但我只是错过了一个愚蠢的陈述。现在,当我尝试打印返回的数组时,我得到的只是一个“NoneType”对象。
非常感谢。
#Quicksort Algorithm
def quicksort(list):
n = len(list)
return recquick(list, 0, n-1)
#Recursive Quicksort Function
def recquick(list, first, last):
#Base Case
if first >= last:
return
else:
#Save pivot value
pivot = list[first]
pos = partseq(list, first, last)
#Repeat the process on the two subsequences
recquick(list, first, pos-1)
recquick(list, pos+1, last)
#Partitions subsequence using first key as pivot
def partseq(list, first, last):
#Save pivot value
pivot = list[first]
#Find pivot position and move elements around the pivot
left = first + 1
right = last
while left <= right:
#Find the first key larger than the pivot
while left < right and list[left] < pivot:
left += 1
#find the last key in the sequence that is smaller than the pivot
while right >= left and list[right] >= pivot:
right -= 1
#Swap the two keys
if left < right:
tmp = list[left]
list[left] = list[right]
list[right] = tmp
#Put the pivot in the proper position
if right != first:
list[first] = list[right]
list[right] = pivot
#Return index position of the pivot value
return right