我的下面的 python 合并排序代码有什么问题吗?

发布于 2025-01-12 16:39:52 字数 609 浏览 0 评论 0原文

合并排序

它显示错误,我无法弄清楚请帮助进行合并排序,,它显示出索引,但我无法弄清楚如何请任何人更新并请帮助我?

def mergesort(a,low,high):
    if(low<high):
        mid=(high+low)//2
        mergesort(a,low,mid)
        mergesort(a,mid+1,high)
        merge(a,low,mid,high)
def merge(a,low,mid,high):
    i=low
    j=mid+1
    while(i<=mid and j<=high):
        if(a[i]<=a[j]):
            print(a[i])
            c.append(a[i])
            i+=1
        else:
            c.append(a[j])
            j+=1
a=[4,2,7,0,9,6,5]
c=[]
mergesort(a,0,len(a))
print(c)
    

我的合并排序算法不起作用

merge sort

it is showing error i cant able to figure out please help in merge sort,, It is showing out of index but i cant figure it out how please could anyone update and please help me?

def mergesort(a,low,high):
    if(low<high):
        mid=(high+low)//2
        mergesort(a,low,mid)
        mergesort(a,mid+1,high)
        merge(a,low,mid,high)
def merge(a,low,mid,high):
    i=low
    j=mid+1
    while(i<=mid and j<=high):
        if(a[i]<=a[j]):
            print(a[i])
            c.append(a[i])
            i+=1
        else:
            c.append(a[j])
            j+=1
a=[4,2,7,0,9,6,5]
c=[]
mergesort(a,0,len(a))
print(c)
    

my merge sort algo not working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2025-01-19 16:39:52

修复索引后,还会出现其他问题。 “合并排序”应该就地进行排序,修改原始列表。你没有那样做;您正在尝试建立一个新列表。这意味着您需要返回您创建的列表,并在下一个过程中使用它。

那么,c 就不能是全局的。它是“合并”函数的本地函数,因此每个调用都有它自己的副本。最后,每个合并步骤仅处理列表的一部分。您必须复制未更改的部分。这似乎有效:

def mergesort(a,low,high):
    if low<high:
        mid=(high+low)//2
        a = mergesort(a,low,mid)
        a = mergesort(a,mid+1,high)
        a = merge(a,low,mid,high)
    return a
def merge(a,low,mid,high):
    c = a[:low]
    i=low
    j=mid+1
    while i<=mid and j<=high:
        if a[i]<=a[j]:
            print(a[i])
            c.append(a[i])
            i+=1
        else:
            print(a[i])
            c.append(a[j])
            j+=1
    c.extend( a[i:mid+1] )
    c.extend( a[j:] )
    return c

a=[4,2,7,0,9,6,5]
c = mergesort(a,0,len(a)-1)
print(c)

After you fix the indexing, there are other problems. "Mergesort" is supposed to do a sort in place, modifying the original list. You're not doing that; you're trying to build up a new list. That means you need to RETURN the list you've created, and use that in the next pass.

Then, c cannot be a global. It's local to the "merge" function, so each call has it's own copy. Finally, each merge step only processed part of the list. You have to copy over the parts you aren't changing. This seems to work:

def mergesort(a,low,high):
    if low<high:
        mid=(high+low)//2
        a = mergesort(a,low,mid)
        a = mergesort(a,mid+1,high)
        a = merge(a,low,mid,high)
    return a
def merge(a,low,mid,high):
    c = a[:low]
    i=low
    j=mid+1
    while i<=mid and j<=high:
        if a[i]<=a[j]:
            print(a[i])
            c.append(a[i])
            i+=1
        else:
            print(a[i])
            c.append(a[j])
            j+=1
    c.extend( a[i:mid+1] )
    c.extend( a[j:] )
    return c

a=[4,2,7,0,9,6,5]
c = mergesort(a,0,len(a)-1)
print(c)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文