【发布时间】:2015-05-10 10:53:43
【问题描述】:
我正在编写的排序程序出现问题,错误
Traceback (most recent call last):
File "/Users/Shaun/PycharmProjects/Sorts/BubbleSort.py", line 117, in <module>
sorted = quick_sort(unsorted, 0, len(unsorted - 1))
TypeError: unsupported operand type(s) for -: 'list' and 'int'
发生在我的快速排序的函数调用中,见下文
print("You chose Quick Sort\n")
sorted = []
sorted = quick_sort(unsorted, 0, len(unsorted - 1))
这里是快速排序函数及其输入参数
def quick_sort(list, leftBound, rightBound) -> object:
leftBound = int(leftBound)
rightBound = int(rightBound)
pivot = int((list[math.floor(leftBound + rightBound / 2)]))
print(pivot)
while leftBound <= rightBound:
# while bigger numbers are above pivot and lower are below
# update bounds left + , right -
while list[leftBound] < pivot:
leftBound += 1
while list[rightBound] > pivot:
rightBound -= 1
if (leftBound <= rightBound):
list[rightBound], list[leftBound] = list[leftBound], list[rightBound]
leftBound += 1
rightBound -= 1
if (leftBound < rightBound):
quick_sort(list, leftBound, rightBound)
if (rightBound < leftBound):
quick_sort(list, leftBound, rightBound)
print(list)
return list
【问题讨论】:
标签: python function sorting call