[英]Python QuicKSort code error
this is quick sort code i found on http://interactivepython.org/runestone/static/pythonds/SortSearch/TheQuickSort.html 这是我在http://interactivepython.org/runestone/static/pythonds/SortSearch/TheQuickSort.html上找到的快速排序代码
its giving error on line 5 ? 它在第5行给出错误? whats wrong ?
怎么了 ? please help/.
请帮忙/。
def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)
def quickSortHelper(alist,first,last):
if first= 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
alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)
Use ==
to test for equality. 使用
==
测试是否相等。 =
is for variable assignment =
用于变量分配
The code on that website is broken. 该网站上的代码已损坏。 You cannot even run it on the site itself.
您甚至无法在网站本身上运行它。 Here is the correct code .
这是正确的代码。 I ran it without errors.
我运行它没有错误。
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:
alist[leftmark],alist[rightmark]=alist[rightmark],alist[leftmark]
alist[first],alist[rightmark]= alist[rightmark],alist[first]
return rightmark
alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)
>>>[17, 20, 26, 31, 44, 54, 55, 77, 93]
I fixed the example on the site. 我将示例固定在网站上。 It works now.
现在可以使用了。 @rollietikes had the correct solution.
@rollietikes具有正确的解决方案。 It appears that the editor "ate" the < and continued to consume characters thinking it was an html tag.
看来编辑器“吃”了<并继续消耗字符,认为这是一个html标签。 This was a bug that we fixed months ago, but I should have rebuilt this book.
这是我们几个月前修复的错误,但我应该重新编写这本书。
Brad 布拉德
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.