I want to code merge sort in Python 3.
Here is my code:
import math
def 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+=1
def 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)
I tested first function(Merge) it works as it should. But I have a problem with the recursion. I cannot figure out where is the problem. The error is "maximum recursion depth exceeded in comparison".