合并排序,子列表保存在哪里?

时间:2016-07-02 13:14:11

标签: python algorithm sorting

我已经包含了我正在使用的代码。

我知道代码的开头会继续拆分子列表,直到您拥有单个值。单个值保存在变量lefthalf和righthalf中,然后按排序顺序合并。

代码如何合并两个元素的两个列表?我认为这些子列表保存在名为alist的单独局部变量中,这些变量是如何合并的?

def mergeSort(alist):

    if len(alist)>1:
        mid = len(alist)//2
        lefthalf = alist[:mid]
        righthalf = alist[mid:]

        mergeSort(lefthalf)
        mergeSort(righthalf)

        i=0
        j=0
        k=0

        while i < len(lefthalf) and j < len(righthalf):
            if lefthalf[i] < righthalf[j]:
                alist[k]=lefthalf[i]
                i=i+1
            else:
                alist[k]=righthalf[j]
                j=j+1
            k=k+1


        while i < len(lefthalf): 
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1


        while j < len(righthalf):
            alist[k]=righthalf[j]
            j=j+1
            k=k+1

2 个答案:

答案 0 :(得分:3)

第一个var server = new Server(@"C:\path\to\bin\browsermob-proxy.bat", 9090); 循环从whilelefthalf中挑选一个较小的较小项目,直到两个列表中的一个用完为止。 (righthalflefthalf每个都包含已排序的项目)

righthalf

第二个,第三个while i < len(lefthalf) and j < len(righthalf): if lefthalf[i] < righthalf[j]: # Pick the smallest item from `leftHalf` if has a smaller item alist[k]=lefthalf[i] i=i+1 else: # Pick smallest item from `rightHalf` alist[k]=righthalf[j] j=j+1 k = k + 1 # `k` keeps track position of `alist` # (where to put the smallest item) # Need to increase the `k` for the next item 循环将剩余项目复制到while。只执行两个循环体中的一个;一个在第一个alist循环中被排除。

SIDE注意:while更改alist[k] = ...的项目。

答案 1 :(得分:3)

所以这是mergeSort的一个原位版本,因为它接受一个列表并将其修改为一个排序版本。 (虽然它涉及创建每一半的副本,因此它不是恒定的空间)。这里评论代码正在做什么:

def mergeSort(alist):

    if len(alist)>1:  # empty lists and lists of one element are sorted already
        mid = len(alist)//2  # find the halfway point
        lefthalf = alist[:mid]  # make a copy of the first half of the list
        righthalf = alist[mid:] # make a copy of the second half of the list 

        mergeSort(lefthalf)
        mergeSort(righthalf)

        # Each half is now sorted

        # Now we're going to copy elements from lefthalf and righthalf
        # back into alist. We do this by keeping three index variables:
        # where we are in lefthalf (i), where we are in righthalf (j)
        # and where we are going to put stuff in alist (k)

        i=0
        j=0
        k=0

        while i < len(lefthalf) and j < len(righthalf):
            # that is, "while we still have stuff left in both halves":

            if lefthalf[i] < righthalf[j]:
                # The thing we're looking at in lefthalf is smaller
                # than what we have in righthalf. Copy the thing in
                # lefthalf over to alist, and increment i
                alist[k]=lefthalf[i]
                i=i+1
            else:
                # same story, but on the right half
                alist[k]=righthalf[j]
                j=j+1
            k=k+1


        # If we ran out of stuff in righthalf first, finish up copying
        # over all the rest of the stuff in lefthalf
        while i < len(lefthalf): 
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1


        # If we ran out of stuff in lefthalf first, finish up copying
        # over all the rest of the stuff in righthalf
        while j < len(righthalf):
            alist[k]=righthalf[j]
            j=j+1
            k=k+1

        # Note that only one of those while loops will actually do anything -
        # the other while loop will have its condition false the first time