[英]merge sort/recursion in python
I understand how merge sort work but however when I try to implement in python, I think I am still a little confused about how things are really working out on stack. 我了解合并排序的工作原理,但是当我尝试在python中实现时,我仍然对事情在堆栈上的工作方式感到困惑。 I have a function called merge_sorted_list which is used for merging two sorted list and a function called merge_sort1 and merge_sort2, which are accounted for the recursion process.
我有一个称为merge_sorted_list的函数,该函数用于合并两个排序的列表,还有一个称为merge_sort1和merge_sort2的函数,这些函数用于递归过程。 "merge_sort1" and "merge_sort2" are very similar but only merge_sort2 gave the correct answer.
“ merge_sort1”和“ merge_sort2”非常相似,但只有merge_sort2给出了正确的答案。 It looks like merge_sort1 was not taking the returned value at each level of the stack.
看起来merge_sort1没有在堆栈的每个级别上获取返回值。 Could anyone tell me what is the difference in the evaluation between merge_sort1 and merge_sort2 please?
谁能告诉我在merge_sort1和merge_sort2之间的评估有什么区别?
def merge_sorted_list(list1,list2):
if(len(list1) == 0 or len(list2) == 0):
print('input error')
return;
i = 0
j = 0
k = 0
list_merge = [0]*(len(list1) + len(list2))
while(i < len(list1) and j < len(list2)):
if (list1[i] < list2[j]):
list_merge[k] = list1[i]
i += 1
else:
list_merge[k] = list2[j]
j += 1
k += 1
while(i < len(list1)):
list_merge[k] = list1[i]
i += 1
k += 1
while (j < len(list2)):
list_merge[k] = list2[j]
j += 1
k += 1
return(list_merge)
def merge_sort1(mylist):
print("splitting ",mylist)
if (len(mylist) > 1):
mid = len(mylist)//2
merge_sort1(mylist[:mid])
merge_sort1(mylist[mid:])
# lefthalf = mylist[:mid]
# righthalf = mylist[mid:]
# merge_sort1(lefthalf)
# merge_sort1(righthalf)
mylist[:] = merge_sorted_list(mylist[:mid],mylist[mid:])
print("merging result ",mylist)
return mylist
def merge_sort2(mylist):
print("splitting ",mylist)
if (len(mylist) > 1):
mid = len(mylist)//2
# merge_sort2(mylist[:mid])
# merge_sort2(mylist[mid:])
lefthalf = mylist[:mid]
righthalf = mylist[mid:]
merge_sort2(lefthalf)
merge_sort2(righthalf)
mylist[:] = merge_sorted_list(lefthalf,righthalf)
print("merging result ",mylist)
return mylist
if we try merge_sort1([4,67,3,3,2,6]), the output will be 如果我们尝试merge_sort1([4,67,3,3,2,6]),输出将是
('splitting ', [4, 67, 3, 3, 2, 6])
('splitting ', [4, 67, 3])
('splitting ', [4])
('splitting ', [67, 3])
('splitting ', [67])
('splitting ', [3])
('merging result ', [3, 67])
('merging result ', [4, 67, 3])
('splitting ', [3, 2, 6])
('splitting ', [3])
('splitting ', [2, 6])
('splitting ', [2])
('splitting ', [6])
('merging result ', [2, 6])
('merging result ', [2, 3, 6])
('merging result ', [3, 2, 4, 6, 67, 3])
The output of merge_sort2([4,67,3,3,2,6]) will be merge_sort2([4,67,3,3,2,6])的输出将为
('splitting ', [4, 67, 3, 3, 2, 6])
('splitting ', [4, 67, 3])
('splitting ', [4])
('splitting ', [67, 3])
('splitting ', [67])
('splitting ', [3])
('merging result ', [3, 67])
('merging result ', [3, 4, 67])
('splitting ', [3, 2, 6])
('splitting ', [3])
('splitting ', [2, 6])
('splitting ', [2])
('splitting ', [6])
('merging result ', [2, 6])
('merging result ', [2, 3, 6])
('merging result ', [2, 3, 3, 4, 6, 67])
Thank you very much 非常感谢你
mylist[:mid]
returns a list. mylist[:mid]
返回一个列表。 In list_sort1
you are (attempting to) sort this list and then promptly discarding it. 在
list_sort1
您正在(尝试)对该列表进行排序,然后立即将其丢弃。 Later on, you are using a second (unmodified) instance of mylist[:mid]
. 稍后,您将使用
mylist[:mid]
的第二个(未修改的)实例。 Etc. for mylist[mid:]
. 等等,用于
mylist[mid:]
。 On the other hand, in the second version you are assigning the results of mylist[:mid]
etc. to variables and hence not discarding the result of sorting them. 另一方面,在第二个版本中,您将
mylist[:mid]
等的结果分配给变量,因此不会丢弃对它们进行排序的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.