Python 考虑在以下列表中运行合并算法:[9,5,7,3,2,1,6,8 ] def merge_sort(未排序的列表): “”(对象列表)->对象列表返回未排序的_列表的副本 使用合并排序算法进行排序 ''' #基本情况:一个空列表已排序,只需返回它 如果(未排序的_列表==[]): 结果=[] #基本情况2:一个包含单个元素的列表被排序,只需返回它 #问题:我们如何改进基本案例? elif(len(未排序的_列表)==1): 结果=未排序的列表 其他: 中点=len(未排序的_列表)//2 #将列表拆分为两个较小的列表 u1=未排序的_列表[:中点] u2=未排序的_列表[中点:] #对每个列表进行递归排序 s1=合并\排序(u1) s2=合并\排序(u2) #合并两个已排序的列表 排序列表=合并(s1、s2) 结果=已排序的列表 返回结果 def合并(s1、s2): ''(对象列表,对象列表)->对象列表 将已排序列表s1和s2合并到单个已排序列表中并返回它 ''' #index1和index2将分别占据我们目前在s1和s2的位置 i1=0 i2=0 #通过s1和s2循环,比较电流指示,并移动 #排序列表中较大的元素 已排序的_列表=[] 而(i1
问题: 考虑在以下列表中运行MergeSort算法: [9,5,7,3,2,1,6,8] 在排序过程中将调用哪些子列表合并? 例如,if merge将被调用两次,一次用于合并列表[a,b,c]和[d,e,f],一次用于 合并列表[x,y,z]和[w],您的答案将是: 合并([a,b,c],[d,e,f]) 合并([x,y,z],[w])Python 考虑在以下列表中运行合并算法:[9,5,7,3,2,1,6,8 ] def merge_sort(未排序的列表): “”(对象列表)->对象列表返回未排序的_列表的副本 使用合并排序算法进行排序 ''' #基本情况:一个空列表已排序,只需返回它 如果(未排序的_列表==[]): 结果=[] #基本情况2:一个包含单个元素的列表被排序,只需返回它 #问题:我们如何改进基本案例? elif(len(未排序的_列表)==1): 结果=未排序的列表 其他: 中点=len(未排序的_列表)//2 #将列表拆分为两个较小的列表 u1=未排序的_列表[:中点] u2=未排序的_列表[中点:] #对每个列表进行递归排序 s1=合并\排序(u1) s2=合并\排序(u2) #合并两个已排序的列表 排序列表=合并(s1、s2) 结果=已排序的列表 返回结果 def合并(s1、s2): ''(对象列表,对象列表)->对象列表 将已排序列表s1和s2合并到单个已排序列表中并返回它 ''' #index1和index2将分别占据我们目前在s1和s2的位置 i1=0 i2=0 #通过s1和s2循环,比较电流指示,并移动 #排序列表中较大的元素 已排序的_列表=[] 而(i1,python,algorithm,Python,Algorithm,问题: 考虑在以下列表中运行MergeSort算法: [9,5,7,3,2,1,6,8] 在排序过程中将调用哪些子列表合并? 例如,if merge将被调用两次,一次用于合并列表[a,b,c]和[d,e,f],一次用于 合并列表[x,y,z]和[w],您的答案将是: 合并([a,b,c],[d,e,f]) 合并([x,y,z],[w]) 我不明白。我得到了合并排序算法,我只是不知道它要求什么 我的答案不是合并([3,5,7,9],[1,2,6,8]) [9,5,7,3,2,1,6,8] [9,
我不明白。我得到了合并排序算法,我只是不知道它要求什么 我的答案不是合并([3,5,7,9],[1,2,6,8]) [9,5,7,3,2,1,6,8] [9,5,7,3][2,1,6,8] [9,5][7,3] [9] [5] 或者将被合并([9],[5]) 合并([7],[3]) 如果我们按顺序走
def merge_sort(unsorted_list):
'''(list of objects) -> list of objects Return a copy of unsorted_list
sorted using the MergeSort algorithm
'''
#Base Case: an empty list is sorted, just return it
if(unsorted_list == []):
result = []
#Base Case 2: a list with a single element is sorted, just return it
#Question: How can we improve our base cases?
elif(len(unsorted_list) == 1):
result = unsorted_list
else:
midpoint = len(unsorted_list) //2
#split the list into two smaller lists
u1 = unsorted_list[:midpoint]
u2 = unsorted_list[midpoint:]
#recursively sort each list
s1 = merge_sort(u1)
s2 = merge_sort(u2)
#merge the two sorted lists
sorted_list = merge(s1,s2)
result = sorted_list
return result
def merge(s1, s2):
'''(list of objects, list of objects) -> list of objets
Merge sorted lists s1 and s2 into a single sorted list and return it
'''
#index1 and index2 will hold our current place in s1 and s2 respectively
i1 = 0
i2 = 0
#loop through s1 and s2 comparing the current indicies, and moving
#the greater element into the sorted list
sorted_list = []
while(i1 < len(s1) and i2 < len(s2)):
if(s1[i1] < s2[i2]):
sorted_list.append(s1[i1])
i1 +=1
else:
sorted_list.append(s2[i2])
i2 +=1
#append the rest of whichever list has not been finished to the end of
#the sorted list
if(i1 < len(s1)):
sorted_list += s1[i1:]
else:
sorted_list += s2[i2:]
return sorted_list
这是自底向上的合并排序方法。将列表分解为n个子列表,并在两个缓冲区之间反复合并子列表[Wiki定义]
在这种情况下。递归将一直发生,直到只剩下前两个元素,然后对它们进行简单排序。然后对接下来的两个元素进行简单排序(检查基本情况#2)。然后5,9和3,7被合并成[3,5,7,9]
,同样的过程也发生在列表的后半部分<代码>[2,1,6,8]作为最后一步,将两个排序列表合并,形成最终排序列表
python -i merge.py
>>> merge_sort([9,5,7,3,2,1,6,8])
[9] [5]
[7] [3]
[5, 9] [3, 7]
[2] [1]
[6] [8]
[1, 2] [6, 8]
[3, 5, 7, 9] [1, 2, 6, 8]
[1, 2, 3, 5, 6, 7, 8, 9]
>>>