The problem is here:
merge_sort(A, first, middle) merge_sort(A, middle+1, last)
You only sort the second part from the middle + 1, when you should start in the middle. In fact, you never reorder an element in the middle.
Of course you cannot write
merge_sort(A, first, middle) merge_sort(A, middle, last)
because when last = first + 1, you first get the middle == and plunge into infinite recursion (stops at RuntimeError: maximum recursion depth exceeded )
So the way:
merge_sort(A, first, middle) if middle > first: merge_sort(A, middle, last)
After this small change, your implementation produces the correct results.
source share