<p>合并函数就是问题所在,应该更像:</p>
<pre><code>def merge(Lsort, Rsort):
sort = []
i = 0
j = 0
while (len(Lsort) > i) & (len(Rsort) > j):
if Lsort[i] < Rsort[j]:
sort.append(Lsort[i])
i += 1
else:
sort.append(Rsort[j])
j += 1
if len(Lsort) > 0:
sort += Lsort[i:]
if len(Rsort) > 0:
sort += Rsort[j:]
return sort
</code></pre>
<p>一般来说,您不需要为排序分配空间,只需在运行时附加到空列表中即可。对于while逻辑,您需要确保没有超出Lsort和Rsort数组的界限。确保索引小于数组长度。你知道吗</p>
<p>当任一数组用完时,您可以将剩余的数组项连接到排序列表中。你知道吗</p>