merging two sorted arrays

rajeevsinghthakur · September 17, 2021 · Python
def mergearry(a1,a2,n1,n2):
    a3 = [None] * (n1+n2)
    i=j=k=0
    while i<n1 and j<n2:
        if a1[i]<a2[j]:
            a3[k] = a1[i]
            i += 1 
            k += 1 
        else:
            a3[k]=a2[j]
            j += 1 
            k += 1 
            
    while i<n1:
        a3[k] = a[i]
        i += 1 
        k += 1 
    while j < n2:
        a3[k] = a2[j]
        j += 1 
        k += 1 
        
        print("Array after merging")
    for i in range(n1 + n2):
        print(str(a3[i]), end = " ")
 
# Driver code
a1 = [1, 3, 5, 7]
n1 = len(a1)
 
a2 = [2, 4, 6, 8]
n2 = len(a2)
mergearry(a1, a2, n1, n2);
 
            
    

Comments

Please sign up or log in to contribute to the discussion.