【发布时间】:2019-08-21 14:04:48
【问题描述】:
我正在尝试使用 python 在单链表中实现快速排序。在我的 quicksort_recur() 中,newhead 是 Node() 类型,没有值,但 newhead 不会在 partition() 中更新其值
class Node(object):
def __init__(self, data):
super(Node, self).__init__()
self.data = data
self.next=None
class Linkedlist(object):
def __init__(self):
self.head = None
#Returns the last node of the list
def get_tail(self, node):
while node.next:
node = node.next
return node
#Partitions the list taking the last element as the pivot
def partition(self,head,end,newhead,newend):
pivot=end
prev=None
curr=head
tail=pivot
# During partition, both the head and end of the list might change
# which is updated in the newHead and newEnd variables
while curr is not pivot:
if curr.data<pivot.data:
# First node that has a value less than the pivot - becomes the new head
if newhead is None:
newhead=curr
prev=curr
curr=curr.next
else:
#If cur node is greater than pivot
#Move cur node to next of tail, and change tail
if prev:
prev.next=curr.next
temp=curr.next
curr.next=None
tail.next=curr
tail=curr
curr=temp
# If the pivot data is the smallest element in the current list,
# pivot becomes the head
if newhead is None:
newhead=pivot
newend=tail
return pivot
#here the sorting happens exclusive of the end node
def quicksort_recur(self,head,end):
lists=Linkedlist()
if head is None or head is end:
return head
newhead=Node(None)
newend=Node(None)
#Partition the list, newHead and newEnd will be updated by the partition function
pivot=lists.partition(head,end,newhead,newend)
# If pivot is the smallest element - no need to recur for the left part
if newhead is not pivot:
#Set the node before the pivot node as NULL
temp=newhead
while temp.next is not pivot:
temp=temp.next
temp.next=None
#Recur for the list before pivot
newhead=lists.quicksort_recur(newhead,temp)
#Change next of last node of the left half to pivot
temp=lists.get_tail(newhead)
temp.next=pivot
#Recur for the list after the pivot element
pivot.next=lists.quicksort_recur(pivot.next,newend)
return newhead
# The main function for quick sort. This is a wrapper over recursive
# function quickSortRecur()
def quick_sort(self,head):
lists=Linkedlist()
tail=lists.get_tail(head)
head=lists.quicksort_recur(head,tail)
return
lists=Linkedlist()
lists.head=Node(10)
l1=Node(11)
l2=Node(12)
l3=Node(13)
lists.head.next=l1
l1.next=l2
l2.next=l3
lists.quick_sort(lists.head)
10Traceback(最近一次调用最后一次): 文件“C:\Users\Didarul Amin\Desktop\ML algo code\practice_linked_list.py”,第 160 行,在
11 12 13 sort lists.quick_sort(lists.head) File "C:\Users\Didarul Amin\Desktop\ML algo code\practice_linked_list.py", line 130, in quick_sort head=lists.quicksort_recur(head,tail) File "C:\Users\Didarul Amin\Desktop\ML algo code\practice_linked_list.py", line 116, in quicksort_recur temp=temp.next AttributeError: 'NoneType' object has no attribute 'next' [Finished in 0.1s with exit code 1]
'''
【问题讨论】:
-
您是否将有效的头部传递给 quick_sort() ?如果是 noneType 则 get_tail 将在这一行报错 while node.next:
-
请检查编辑并确保它反映了您的实际代码。
-
发布有关产生异常的代码的问题时,请始终包含完整的 Traceback - 复制并粘贴它,然后将其格式化为代码(选择它并输入
ctrl-k) -
您还没有向我们展示使用这些类的代码 - 请阅读minimal reproducible example。
-
您是否尝试过Catch the error 并检查/打印除套件中的相关数据?结果如何?事情是否如你所愿?
标签: python python-3.x quicksort singly-linked-list