gpt4 book ai didi

python - 使用归并排序的反转次数

转载 作者:行者123 更新时间:2023-12-04 08:08:44 26 4
gpt4 key购买 nike

我编写了这段代码来使用 merge_sort 来获取反转次数。 ,但我没有得到正确的输出。有人能告诉我为什么它给出错误的输出吗?

def merge(B,C):  # B,C are sorted
D = list()
count = 0
# empty list to get the merged list
while (B != []) and (C != []): # breaks if any one of B or C becomes empty

b = B[0] # to get first element of B
print("b",b)
c = C[0] # to get first element of C
print("c",c)

if b<=c:
B.remove(b) # if b<c remove b from B and add to D
D.append(b)
else:
C.remove(c) # if b>c remove c from C and add to D
D.append(c)
count += 1
print("count_in_merge",count)

# now if either one of B or C has become empty so while loop is exited
# so we can add remaining elements of B and C as it is to D as they are
# already sorted
if B != []:
for i in B:
D.append(i)

if C != []:
for i in C:
D.append(i)

return D, count

def sort_and_num(A):
if len(A) == 1:
return A,0

m = len(A) // 2

B,count_b = sort_and_num(A[0:m])
C,count_c = sort_and_num(A[m:])

A_,count = merge(B,C)
count += (count_b + count_c)

return A_, count
当我运行时:
A = [ 9, 8 ,7, 3, 2, 1] 
A_,c = sort_and_num(A)
print(A_,c)
输出是:
[1, 2, 3, 7, 8, 9] 9
但输出应该是
[1, 2, 3, 7, 8, 9] 15
另一方面,如果我输入:
A = [3,1,2,4] 
A_, count = sort_and_num(A)
print(A_, count)
输出是:
[1,2,3,4 ] 3 
哪个是正确的。哪里出错了?

最佳答案

您的代码中存在一些问题:

  • 要删除列表的第一个元素,您应该使用 pop() ,不是 remove()
  • C 取元素时的反转次数是 len(B) ,不是 1 .
  • 您应该将每一半的反转次数和合并阶段的反转次数相加
  • sort_and_num 中的初始测试还应该测试一个空列表并返回它的计数为 0。

  • 这是一个修改后的版本:
    def merge(B, count_b, C, count_c):  # B,C are sorted
    D = []
    count = count_b + count_c
    # empty list to get the merged list
    while (B != []) and (C != []): # breaks if any one of B or C becomes empty
    if B[0] <= C[0]:
    D.append(B.pop()) # if b<=c remove b from B and add it to D
    else:
    D.append(C.pop()) # if b>c remove c from C and add it to D
    count += len(B) # moving c before all remaining elements of B

    # now if either one of B or C has become empty so while loop is exited
    # so we can add remaining elements of B and C as it is to D as they are
    # already sorted
    for i in B:
    D.append(i)

    for i in C:
    D.append(i)

    return D, count

    def sort_and_num(A):
    if len(A) <= 1:
    return A,0

    m = len(A) // 2

    B,count_b = sort_and_num(A[:m])
    C,count_c = sort_and_num(A[m:])

    return merge(B, count_b, C, count_c)

    关于python - 使用归并排序的反转次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66088092/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com