2
我想問你一些關於這個項目的幫助我正在研究我在哪裏寫遞歸合併排序函數,它將在鏈表上工作。這就是我迄今爲止的(我沒有把整個代碼放在這裏,只是我正在努力的部分)。遞歸合併在python的鏈表上排序
....
def merge_sort(self):
len_list,len_list2= 0, 0
list=self.head #first half of linked list
list2=self.divide() #function that makes second half of linked list
#from imput like z = SortedList([46, 27, 93, 91, 23])
#i will get list=46->27->93->None
# list2=91->23->
while list is not None:
len_list+=1
list=list.next
while list2 is not None:
len_list2+=1
list2=list2.next
# in len_list,len_list2 i am storing length of these linked lists
def merge(self,left,right):
result,i,j=None,0,0
while (i<len_list) and (j<len_list2):
if list.data < list2.data:
result.append(list.data) #append is function which appends
list=list.next #nodes
i+=1
else:
result.append(list2.data)
list2=list2.next
j+=1
#some returns should follow but i dont know what to do
我很確定這是錯誤的很多級別我只是試圖複製列表的合併排序功能,並將其轉換到鏈接列表上。如果任何人都可以幫助我如何做到這一點,而不需要將定義的參數改爲(def merge_sort(self)),並告訴我如何遞歸地調用它,我將非常感激。事先謝謝你。 同時適用於半區我應該使用功能區劃(個體經營)使鏈表但如果你知道任何其他方式讓我知道