Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursion in Python 3

I was trying to do merge sort in a different way (instead of those available in texts or so..), now I have a successful merge algorithm, but the thing is that when I recursively call on half lists, the merged thing doesn't get updated. Help me with variable life in the recursion.

The output of the below-given code is: -

[9, 5]
After merging [5, 9]
[12, 4]
After merging [4, 12]
[9, 5, 12, 4] #it should be (the updated one i.e  [5, 9, 4, 12])
After merging [5, 9, 12, 4]
[6, 8]
After merging [6, 8]
[45, 2]
After merging [2, 45]
[6, 8, 45, 2]
After merging [6, 8, 45, 2]
[9, 5, 12, 4, 6, 8, 45, 2]
After merging [4, 5, 6, 8, 9, 12, 45, 2]
[9, 5, 12, 4, 6, 8, 45, 2]

def merge(arr1, arr2):
"""
Input is two sorted lists
Output is a single merged list
"""
        for i in arr1:
            for j in list(range(len(arr2))):
                if i<arr2[j]:
                    arr2.append(arr2[-1])
                    for count in list(range(len(arr2)-1, j, -1)):
                        arr2[count] = arr2[count-1]
                    arr2[j] = i
                    break
                if j == len(arr2)-1:
                    arr2.append(i)
        return arr2
    def mergeSort(arr):
        if len(arr) !=1:
            mergeSort(arr[:len(arr)//2])
            mergeSort(arr[len(arr)//2:])
            print(arr)
            arr = merge(arr[:len(arr)//2],arr[len(arr)//2:])
            print("After merging", arr)

        else:
            return arr
    a = [9,5,12, 4, 6, 8,45, 2]
    mergeSort(a)
    print(a)
like image 902
Shanur Rahman Avatar asked May 17 '26 18:05

Shanur Rahman


1 Answers

You should make mergeSort return the merged list, and the caller should output the returning value of mergeSort instead:

def merge(arr1, arr2):
    merged = []
    while arr1 and arr2:
        if arr1[0] > arr2[0]:
            arr1, arr2 = arr2, arr1
        merged.append(arr1.pop(0))
    merged.extend(arr1 or arr2)
    return merged
def mergeSort(arr):
    if len(arr) <= 1:
        return arr
    return merge(mergeSort(arr[:len(arr)//2]), mergeSort(arr[len(arr)//2:]))
a = [9, 5, 12, 4, 6, 8, 45, 2]
print(mergeSort(a))
like image 102
blhsing Avatar answered May 20 '26 07:05

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!