アルゴリズムがどのように機能するかを理解するのが難しい場合は、デバッグ出力を追加して、アルゴリズム内で実際に何が起こっているかを確認します。
ここにデバッグ出力のあるコードがあります。の再帰呼び出しを使用してすべてのステップを理解し、出力がmergesort
どうなるかを理解してください。merge
def merge(left, right):
result = []
i ,j = 0, 0
while i < len(left) and j < len(right):
print('left[i]: {} right[j]: {}'.format(left[i],right[j]))
if left[i] <= right[j]:
print('Appending {} to the result'.format(left[i]))
result.append(left[i])
print('result now is {}'.format(result))
i += 1
print('i now is {}'.format(i))
else:
print('Appending {} to the result'.format(right[j]))
result.append(right[j])
print('result now is {}'.format(result))
j += 1
print('j now is {}'.format(j))
print('One of the list is exhausted. Adding the rest of one of the lists.')
result += left[i:]
result += right[j:]
print('result now is {}'.format(result))
return result
def mergesort(L):
print('---')
print('mergesort on {}'.format(L))
if len(L) < 2:
print('length is 1: returning the list withouth changing')
return L
middle = len(L) / 2
print('calling mergesort on {}'.format(L[:middle]))
left = mergesort(L[:middle])
print('calling mergesort on {}'.format(L[middle:]))
right = mergesort(L[middle:])
print('Merging left: {} and right: {}'.format(left,right))
out = merge(left, right)
print('exiting mergesort on {}'.format(L))
print('#---')
return out
mergesort([6,5,4,3,2,1])
出力:
---
mergesort on [6, 5, 4, 3, 2, 1]
calling mergesort on [6, 5, 4]
---
mergesort on [6, 5, 4]
calling mergesort on [6]
---
mergesort on [6]
length is 1: returning the list withouth changing
calling mergesort on [5, 4]
---
mergesort on [5, 4]
calling mergesort on [5]
---
mergesort on [5]
length is 1: returning the list withouth changing
calling mergesort on [4]
---
mergesort on [4]
length is 1: returning the list withouth changing
Merging left: [5] and right: [4]
left[i]: 5 right[j]: 4
Appending 4 to the result
result now is [4]
j now is 1
One of the list is exhausted. Adding the rest of one of the lists.
result now is [4, 5]
exiting mergesort on [5, 4]
#---
Merging left: [6] and right: [4, 5]
left[i]: 6 right[j]: 4
Appending 4 to the result
result now is [4]
j now is 1
left[i]: 6 right[j]: 5
Appending 5 to the result
result now is [4, 5]
j now is 2
One of the list is exhausted. Adding the rest of one of the lists.
result now is [4, 5, 6]
exiting mergesort on [6, 5, 4]
#---
calling mergesort on [3, 2, 1]
---
mergesort on [3, 2, 1]
calling mergesort on [3]
---
mergesort on [3]
length is 1: returning the list withouth changing
calling mergesort on [2, 1]
---
mergesort on [2, 1]
calling mergesort on [2]
---
mergesort on [2]
length is 1: returning the list withouth changing
calling mergesort on [1]
---
mergesort on [1]
length is 1: returning the list withouth changing
Merging left: [2] and right: [1]
left[i]: 2 right[j]: 1
Appending 1 to the result
result now is [1]
j now is 1
One of the list is exhausted. Adding the rest of one of the lists.
result now is [1, 2]
exiting mergesort on [2, 1]
#---
Merging left: [3] and right: [1, 2]
left[i]: 3 right[j]: 1
Appending 1 to the result
result now is [1]
j now is 1
left[i]: 3 right[j]: 2
Appending 2 to the result
result now is [1, 2]
j now is 2
One of the list is exhausted. Adding the rest of one of the lists.
result now is [1, 2, 3]
exiting mergesort on [3, 2, 1]
#---
Merging left: [4, 5, 6] and right: [1, 2, 3]
left[i]: 4 right[j]: 1
Appending 1 to the result
result now is [1]
j now is 1
left[i]: 4 right[j]: 2
Appending 2 to the result
result now is [1, 2]
j now is 2
left[i]: 4 right[j]: 3
Appending 3 to the result
result now is [1, 2, 3]
j now is 3
One of the list is exhausted. Adding the rest of one of the lists.
result now is [1, 2, 3, 4, 5, 6]
exiting mergesort on [6, 5, 4, 3, 2, 1]
#---