Advertisement
Iam_Sandeep

Untitled

Sep 14th, 2022
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #merge sort on array of structures (C) list of Objects(Python)
  2. class node:
  3.     def __init__(self,age):
  4.         self.age=age
  5.  
  6.  
  7. import random
  8. a=[]
  9. for i in range(10):
  10.     a.append(node(random.randint(4,100)))
  11. for i in a:
  12.     print(i.age,end=" ")
  13. print()
  14.  
  15. def merge(a):
  16.     if len(a)<=1:
  17.         return
  18.     n=len(a)
  19.     m=n//2
  20.     L,R=a[:m],a[m:]
  21.     merge(L)
  22.     merge(R)
  23.     i,j,k=0,0,0
  24.     while i<len(L) and j<len(R):
  25.         if L[i].age<R[j].age:
  26.             a[k]=L[i]
  27.             i=i+1
  28.         else:
  29.             a[k]=R[j]
  30.             j=j+1
  31.         k=k+1
  32.     while i<len(L):
  33.         a[k]=L[i]
  34.         i,k=i+1,k+1
  35.     while j<len(R):
  36.         a[k]=R[j]
  37.         j,k=j+1,k+1
  38. merge(a)
  39. for i in a:
  40.     print(i.age,end=" ")
  41. print()
  42.  
  43.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement