python递归排序_Python-合并排序递归算法

我试图为递归合并排序算法实现此伪代码:**procedure** *mergesort*(L = a1, a2,…,an )

**if** n > 1 **then**

m := ⌊n/2⌋

L1 := a1, a2,…,am

L2 := am+1, am+2,…,an

L := merge(mergesort(L1), mergesort(L2 ))

{L is now sorted into elements in increasing order}

**procedure** *merge*(L1, L2 :sorted lists)

L := empty list

**while** L1 and L2 are both nonempty

remove smaller of first elements of L1 and L2 from its list;

put at the right end of L

**if** this removal makes one list empty

**then** remove all elements from the other list and append them to L

return L {L is the merged list with the elements in increasing order}

目的是在python上编写它,到目前为止我已经对它进行了编码,但是它没有正常运行,每次运行它时都会打印:function merge at 0x0000000002024730。下面是python代码:#Recursive Merge Sort

import math

ent = [10000, 967, 87, 91, 117, 819, 403, 597, 1201, 12090]

def merge(L1, L2):

while len(L1) and len(L2) != 0:

L.append(L1[0])

L1.remove(L1[0])

L.append(L2[0])

L2.remove(L2[0])

if len(L1) == 0:

L.append(L2)

elif len(L2) == 0:

L.append(L1)

return L

def mergesort(ent):

if len(ent)>1:

m=math.floor(len(ent)/2)

L1 = []

L2 = []

L1=L1+ent[:m]

L2=L2+ent[m+1:len(ent)]

L=merge(mergesort(L1),mergesort(L2))

print(merge)

我对函数是如何递归地一起工作有一些疑问,这就是为什么我认为我不能正确地解决和编写它。有什么帮助或建议吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值