[英]Implementation of Quick sort in python and exchanging pivot value
We know that while implementing quick sort we select a pivot value.And during the partition phase we exchange pivot value with the rightmark. 我们知道在实现快速排序时我们会选择一个枢轴值,在分区阶段,我们会用正确的标记交换枢轴值。 Here is my code:
这是我的代码:
def quicksort(mylist):
quicksorthelper(mylist,0,len(mylist)-1)
def quicksorthelper(mylist,first,last):
if first< last:
splitpoint=partition(mylist,first,last)
quicksorthelper(mylist,first,splitpoint-1)
quicksorthelper(mylist,splitpoint+1,last)
def partition(mylist,first,last):
pivot= mylist[first]
leftmark= first +1
rightmark= last
done = False
counter = 0
while not done:
while leftmark <= rightmark and mylist[leftmark]< pivot:
leftmark = leftmark +1
while leftmark <= rightmark and mylist[rightmark]>pivot:
rightmark= rightmark -1
if leftmark>rightmark:
done = True
else:
temp = mylist[leftmark]
mylist[leftmark]=mylist[rightmark]
mylist[rightmark]=temp
counter +=1
temp= pivot #pivot = mylist[first]
pivot = mylist[rightmark]
mylist[rightmark]=temp
return rightmark
mylist= [54,26,93,17,77,31,44,55,20]
quicksort(mylist)
print(mylist)
So the problem is if i write pivot instead of mylist[first] the program is not working whereas if i write mylist[first] in place of pivot while exchanging values with rightmark it just works fine. 因此,问题在于,如果我编写数据透视表而不是mylist [first],则该程序无法正常工作;而如果我在交换带有右标记的值时编写mylist [first]来代替数据透视表,则效果很好。 Can you tell me why is this happening
你能告诉我为什么会这样吗
Also if i try to do something like that: mylist = [54, 26, 93, 17, 77, 31, 44, 55, 20] sortlist=quicksort(mylist) print(sortlist)
Then the output is None .Don't know what is wrong with that 另外,如果我尝试执行以下操作:
mylist = [54, 26, 93, 17, 77, 31, 44, 55, 20] sortlist=quicksort(mylist) print(sortlist)
然后输出为None。知道那是怎么回事
This implementation does not work: 此实现不起作用:
temp= pivot #pivot = mylist[first]
pivot = mylist[rightmark]
mylist[rightmark]=temp
Because you are not mutating mylist
when you do 因为当您这么做时,您并未在改变我的
mylist
pivot = mylist[rightmark]
You are simply assigning a new value to the variable pivot
: 您只需将新值分配给变量
pivot
:
>>> i = 2
>>> j = 4
>>> somelist = ['a','b','c','d','e','f','g']
>>> pivot = somelist[i]
>>> pivot
'c'
>>> temp = pivot
>>> pivot = somelist[j]
>>> pivot
'e'
>>> somelist[j] = temp
>>> pivot
'e'
>>> somelist
['a', 'b', 'c', 'd', 'c', 'f', 'g']
For the same reason, doing the following does not change the list: 出于相同的原因,执行以下操作不会更改列表:
>>> anotherlist = [1, 2, 3]
>>> x = anotherlist[1]
>>> x
2
>>> x = 53
>>> x
53
>>> anotherlist
[1, 2, 3]
You must do: 您必须做:
>>> anotherlist[0] = 53
>>> anotherlist
[53, 2, 3]
>>>
Or you could use a mutator method. 或者,您可以使用mutator方法。
Finally, you do not need a temp
variable to do a swap in Python: 最后,您不需要
temp
变量即可在Python中进行交换:
>>> a = 42
>>> b = 88
>>> a,b = b,a
>>> a
88
>>> b
42
>>>
Or with a list: 或列出:
>>> somelist = ['a','b','c','d','e','f','g']
>>> i = 2
>>> j = 4
>>> somelist[i], somelist[j] = somelist[j], somelist[i]
>>> somelist
['a', 'b', 'e', 'd', 'c', 'f', 'g']
Regarding your last question: quicksort does not return anything. 关于您的最后一个问题:quicksort不返回任何内容。 Thats why
sortlist=quicksort(mylist)
sets sortlist to None
. 这就是为什么
sortlist=quicksort(mylist)
将sortlist设置为None
。
Read How do I pass a variable by reference? 阅读如何通过引用传递变量? to understand whats going on.
了解发生了什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.