以下是我的代码抛出的错误:
File "python", line 39, in <module>
File "python", line 8, in quicksort
File "python", line 8, in quicksort
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
下面是我的快速排序代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
def quicksort(arr, beg, end):
if beg < end:
pivot = partition(arr, beg, end)
quicksort(arr, beg, pivot - 1)
quicksort(arr, pivot + 1, end)
def partition(arr, beg, end):
left = beg
right = end
loc = beg
while arr[loc] <= arr[right] and loc != right:
right = right - 1
if loc == right:
return loc
elif arr[loc] > arr[right]:
(arr[loc], arr[right]) = (arr[right], arr[loc])
loc = right
while arr[loc] >= arr[left] and loc != left:
left = left + 1
if loc == left:
return loc
elif arr[loc] < arr[left]:
(arr[loc], arr[left]) = (arr[left], arr[loc])
loc = left
y = [9,7,5,1,5]
quicksort(y, 0, 4)
print(y)
宝慕林4294392
FFIVE
相关分类