On smaller lists of up to size N = ~1950 or so, I get the correct output... however, list sizes that are not much larger return an error rather than the expected result. My code:
def merge(left, right, aux=[]):
if left == []:
for x in right:
aux.append(x)
return aux
elif right == []:
for x in left:
aux.append(x)
return aux
elif left[0] > right[0]:
aux.append(right[0])
return merge(left, right[1:], aux)
else:
aux.append(left[0])
return merge(left[1:], right, aux)
def msort(values):
size = len(values)
if size == 1:
return values
else:
mid = size/2
return merge(msort(values[:mid]), msort(values[mid:]), [])
Running msort
on these test lists give me the expected (ascending order) output:
val = [x for x in range(1950, 0, -1)
val = [x for x in range(4,0,-1)]
e.g. [1,2,3,...,1948,1949,1950] and [1,2,3,4]
However, when I run msort
on this test case:
val = [x for x in range(2000,0,-1)]
I now receive this error (after numerous tracebacks to merge
):
RuntimeError: maximum recursion depth exceeded in cmp
So my question is: What went wrong with my implementation here? I can't use it with lists of N ~ >=2000. Why?