0
我試過並行合併排序在Python 2.7中,但我不能這樣做。因爲我不知道是否應該使用線程或多處理來實現。請在此線程或多處理代碼中寫入並行代碼:python 2.7如何並行合併排序?
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])
謝謝。
你知道這段代碼沒有運行,對不對?要麼你從任何地方複製和粘貼它,要麼你搞砸了格式 – byxor
「請在線程中編寫並行代碼或對這些代碼進行多處理」這是一個命令嗎? – TigerhawkT3
@ TigerhawkT3我正想問同樣的問題:) – mutantkeyboard