我发现这种合并排序解决方案在线,我想知道如果while循环是要走的路或如果也有使用2 for循环和比较这些。Python:结合两个排序列表(并保持排序),而不使用内置排序
def merge(l, m):
result = []
i = j = 0
total = len(l) + len(m)
while len(result) != total:
if len(l) == i:
result += m[j:]
break
elif len(m) == j:
result += l[i:]
break
elif l[i] < m[j]:
result.append(l[i])
i += 1
else:
result.append(m[j])
j += 1
print result
合并([1,2,6,7],[1,3,5,9])
啊,gotchya。所以几乎没有k,你创建自己的“模拟”循环(i + = 1&j + = 1)? –
@IntrepidDiamond把k工作,现在它打印出insersion的索引 – LetzerWille