我在python中編寫了合併排序算法。它完美的工作,直到10000數字,但10000後,它給我分段錯誤11.可能是什麼問題?任何想法關於它合併排序算法失敗
def merge_count(arr):
if len(arr) < 2:
return (arr, 0)
m = int(len(arr)/2)
left, l_counter = merge_count(arr[:m])
right, r_counter = merge_count(arr[m:])
return merge(left, right, l_counter + r_counter)
def merge(left, right, counter):
if len(left) * len(right) == 0:
return (left + right, counter)
if left[len(left) - 1] > right[len(right) - 1]:
val = left.pop(len(left) - 1)
counter += len(right)
else:
val = right.pop(len(right) - 1)
arr, counter = merge(left, right, counter)
return (arr + [val], counter)
代碼在哪裏? – RaviH
分割錯誤?在Python中?你究竟做了什麼?向我們展示代碼。 – user2357112
我加了我的代碼@ user2357112 –