我想在Python 3中进行代码合并排序。这是我的代码:import mathdef Merge(array,start,mid,end): n1 = mid - start +1 n2 = end - mid i = 0 j = 0 left = [None] * n1 right = [None] * n2 inv = 0 for i in range(0,n1): left[i] = array[start + i - 1] for j in range(0,n2): right[j] = array[mid + j] left.append(math.inf) right.append(math.inf) new_list = [] for k in range(0,end): if left[i] <= right[j]: array[k] = left[i] i+=1 elif left[i] > right[j]: array[k] = right[j] j+=1def MergeSort(array,start,end): if len(array) <= 1: return array mid = math.ceil((start + end)/2) MergeSort(array,start,mid) MergeSort(array,mid+start,end) Merge(array,start,mid,end)stuff = [1, -5, 17, 32, 6] MergeSort(stuff, 0, len(stuff))print(stuff)我测试了第一个功能(合并),它可以正常工作。但是我对递归有问题。我不知道问题出在哪里。错误是“比较中超出了最大递归深度”。
添加回答
举报
0/150
提交
取消