我对以下代码的以下输出感到困惑:
def msort3(x):
print("splitting:",x)
result = []
if len(x) < 2:
print("merging:",x)
return x
mid = int(len(x) / 2)
y = msort3(x[:mid])
print(y)
z = msort3(x[mid:]) ## this will be run when x is 87
print(z)
i = 0
j = 0
while i < len(y) and j < len(z):
if y[i] > z[j]:
result.append(z[j])
j += 1
else:
result.append(y[i])
i += 1
result += y[i:]
result += z[j:]
print("result is ", result)
return result
if __name__ == '__main__':
print("hi", msort3([17,87,6,22]))
一些输出是:
splitting: [17, 87, 6, 22]
splitting: [17, 87]
splitting: [17]
merging: [17]
[17]
splitting: [87]
merging: [87]
[87]
result is [17, 87]
[17, 87]
splitting: [6, 22]
现在它是怎么读的[6, 22]
?我想当我们得到时递归调用已经完成result = [ 17,87]
.
请帮忙,我很困惑并且已经在这里跟踪了代码http://interactivepython.org/courselib/static/pythonds/SortSearch/TheMergeSort.html http://interactivepython.org/courselib/static/pythonds/SortSearch/TheMergeSort.html.