Advertisement
cd62131

merge sort

Dec 29th, 2018
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import sys
  3.  
  4.  
  5. def merge(p, q):
  6.     s = []
  7.     while 0 < len(p) and 0 < len(q):
  8.         a = p[0]
  9.         b = q[0]
  10.         if a <= b:
  11.             s.append(a)
  12.             p.pop(0)
  13.         else:
  14.             s.append(b)
  15.             q.pop(0)
  16.     if 0 < len(p):
  17.         s.extend(p)
  18.     elif 0 < len(q):
  19.         s.extend(q)
  20.     return s
  21.  
  22.  
  23. def mergesort(a):
  24.     n = len(a)
  25.     if n <= 1:
  26.         return a
  27.     return merge(mergesort(a[:n // 2]), mergesort(a[n // 2:]))
  28.  
  29.  
  30. a = list(map(int, sys.argv[1:]))
  31. print(a)
  32. print(mergesort(a))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement