Search code examples
pythonsortingmergesort

MergeSort in python


def merge(A,l,m,r):

    i = l
    j = m+1
    new = []

    while i <= m and j <= r:
        if A[i] <= A[j]:
            new.append(A[i])
            i += 1
        else:
            new.append(A[j])
            j += 1

    while i <= m:
        new.append(A[i])
        i += 1
   
    while j <= r:
        new.append(A[j])
        j += 1

    return new

This function doesn't work due to an error, can you help me to understand what is the error and how to fix it?


Solution

  • This statement doesn't make sense:

    return new
    

    Since you nowhere use the return of the function. So the function merge() does nothing.

    I believe you need to change the array A directly inside the function. No need to create and return the new array.