简体   繁体   English

合并排序问题-Python

[英]Merge Sort Problems - Python

What's wrong with my code? 我的代码有什么问题? It prints only a part of the vect values. 它仅输出vect值的一部分。 Seems that the while loop breaks at some point. 似乎while循环在某个时刻中断。 I don't understand why. 我不明白为什么。

def print_list(vect):
    for i in range(0, len(vect)):
        print(vect[i])

def merge_sort(vect):
    left = []
    right = []    
    result = []

    for i in range(0, int(len(vect)/2)):
        left.append(vect[i])
    for i in range(int(len(vect)/2), len(vect)):
        right.append(vect[i])

    left.sort()
    right.sort()
    i = 0
    j = 0

    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1

    print(len(result))
    return result

vect = [3, 1, 5, 7, 10, 2, 0]

vect = merge_sort(vect)

Well, your mistake is that after your while loop 好吧,你的错误是你的while循环之后

while i < len(left) and j < len(right):
  ...

it may be (and most probably would) that either i < len(left) or j < len(right) , so you need to append appropriate part's suffix to the answer. i < len(left)j < len(right)可能是(而且很可能是j < len(right) ,因此您需要在答案后面加上适当部分的后缀。 It's easy to do with 这很容易做到

result += left[i:]
result += right[j:]

Explanation: 说明:

Imagine the merge procedure: you have i and j at 0 at start, and at every step you move one of them forward. 想象一下合并过程:您在开始时将i和j设为0,然后在每一步中都将它们之一向前移动。 When you stop ? 什么时候停止? When one of them reaches the end. 当其中之一到达终点时。 Let's say i has reached the end. 假设我已经结束了。 Hereby you added the whole left part to the result, but there are still some elements in right between j and len(right), so you have to add them to the answer too. 因此,您将整个左侧部分添加到了结果中,但是j和len(right)之间的右侧仍然有一些元素,因此您也必须将它们添加到答案中。

Offtop : 台面

You are implementing merge sort, so please have 您正在执行合并排序,所以请

left = merge_sort( left )
right = merge_sort( right )

instead of 代替

left.sort()
right.sort()

Attention: you have to add the following check to your code at the beginning of merge function to avoid infinite recursion: 注意:您必须在合并功能开始时在代码中添加以下检查,以避免无限递归:

if len( vect ) == 1:
   return vect

Also in your print_list function you can just use 同样在print_list函数中,您可以使用

print vect

or at least 或至少

for x in vect
print x

The while loop exits as soon as either left or right is exhausted. 一旦左侧或右侧耗尽,while循环就会退出。 This leaves all the elements left in the unexhausted list unmerged. 这使未用尽列表中剩余的所有元素都不会合并。

Change your code to this: 将代码更改为此:

def print_list(vect):
    for i in range(0, len(vect)):
        print(vect[i])

def merge_sort(vect):
    left = []
    right = []

    result = []
    for i in range(0, int(len(vect)/2)):
        left.append(vect[i])
    for i in range(int(len(vect)/2), len(vect)):
        right.append(vect[i])

    left.sort();
    right.sort();
    i = 0
    j = 0

    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1

    if i < len(left):
        result.extend(left[i:])
    else:
        result.extend(right[j:])

    print(len(result))
    return result

vect = [3, 1, 5, 7, 10, 2, 0]

vect = merge_sort(vect)
print vect

If you're using the sort method on left and right, I'm a little confused as to why you're not just using it on the list as a whole. 如果您在左右使用sort方法,那么为什么您不只在整个列表中使用它,我有点困惑。 But I suppose you have your reasons. 但是我想你有你的理由。 :) :)

Edit: I put the entire block of code there so you can see it. 编辑:我把整个代码块放在那里,以便您可以看到它。 When I run this, it prints [0, 1, 2, 3, 5, 7, 10], which is correct. 当我运行此命令时,它会打印[0,1,2,3,5,7,10],这是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM