【发布时间】:2016-06-13 12:12:30
【问题描述】:
我编写了这个就地快速排序算法,但是修改后的数组没有传递给父调用。我是 Python 新手,不太了解按值/引用传递、可变/不可变的东西等。 任何解释指导都会很棒!
def quickSortPartition(a, l, r):
if len(a) < 2:
return a
else:
print a, l, r
p = a[l]
i = l + 1
for j in range(l + 1, r+1):
if a[j] < p:
temp = a[i]
a[i] = a[j]
a[j] = temp
i = i + 1
temp = a[l]
a[l] = a[i - 1]
a[i - 1] = temp
firstPartition = a[:i-1]
pivot = a[i-1]
secondPartition = a[i:]
if len(a[:i-1]) > 1:
quickSortPartition(a[:i-1], 0, len(a[:i-1])-1)
if len(a[i:]) > 1:
quickSortPartition(a[i:], 0, len(a[i:])-1)
return a
lines = [3, 8, 2, 5, 1, 4, 7, 6]
# print lines
quickSorted = quickSortPartition(lines, 0, len(lines)-1)
print quickSorted
【问题讨论】:
-
您的输入 (testerText.txt) 是什么样的?
-
糟糕,感谢您的评论,刚刚表明
-
发布了解决方案。确保捕获递归函数返回的结果全部
标签: python algorithm sorting recursion quicksort