【问题标题】:Having Problems with MergeSort合并排序有问题
【发布时间】:2017-03-28 21:51:32
【问题描述】:
def merge(L, start_index, sublist_size):
    """
    Merge two sublists of a list L

    Parameters:
    L - the list
    start_index - the index of the first element to be merged
    sublist_size - the size of the chunks to be merged

    Left chunk: L[start_index] to L[start_index + sublist_size - 1]
    Right chunk: L[start_index + sublist_size] to L[start_index + 2 * sublist_size - 1]
    """

    index_left = start_index
    left_stop_index = start_index + sublist_size
    index_right = start_index + sublist_size
    right_stop_index = min(start_index + 2 * sublist_size,
                           len(L))

    print('Merging sublists:', L[index_left:left_stop_index], 'and',
          L[index_right:right_stop_index]);

    L_tmp = []

    while (index_left < left_stop_index and
           index_right < right_stop_index):
        if L[index_left] < L[index_right]:
           L_tmp.append(L[index_left])
           index_left += 1
        else:
           L_tmp.append(L[index_right])
           index_right += 1

    if index_left < left_stop_index:
           L_tmp.extend(L[index_left : left_stop_index])
    if index_right < right_stop_index:
           L_tmp.extend(L[index_right : right_stop_index])

    L[start_index : right_stop_index] = L_tmp
    print('Merged sublist:', L_tmp, '\n')

def merge_sort(L):
    """
    Sort a list L using the merge sort algorithm.

    (Starter code doesn't fully sort the list.)
    """
    left_start_index = 0
    chunksize = 1  # Start by dividing the list into N sub-lists of 1 element each

    while chunksize < len(L):`enter code here`
        print("\n*** Sorting sublists of size", chunksize)
        print(L)

        while left_start_index + chunksize < len(L):
            merge(L, left_start_index, chunksize)

            # Move to next pair of chunks
            left_start_index += 2 * chunksize

        chunksize= chunksize *2
        print('List is now',L)

大家好,完成这段代码有很多困难。代码的 def 合并部分很好,我遇到的问题是 def_merge 排序部分。因此,当排序大小为 1 的子列表时,代码第一次可以正常工作,但我无法让代码在此之后继续合并排序.我觉得好像问题出在蓄能器上。

【问题讨论】:

    标签: python sorting merge


    【解决方案1】:

    merge_sort 函数中,运行内部while 循环后,left_start_index 的值已更改为不是0,因此您的代码无法正确合并排序超出列表中的前两个元素。在内部while循环之后,在chunksize=chunksize*2之前,重置left_start_index=0应该可以解决问题。

    def merge_sort(L):
        ...
        #outer while loop
        while chunksize < len(L):
            ....
            #inner while loop
            while left_start_index + chunksize < len(L):
                merge(L, left_start_index, chunksize)
    
                # Move to next pair of chunks
                left_start_index += 2 * chunksize
            #Reset left_start_index to 0
            left_start_index = 0
            chunksize= chunksize *2
            print('List is now',L)
    

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      相关资源
      最近更新 更多