Advertisement
stiansjogren

mergesorts

Nov 30th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. def mergesort(seq):
  2.     mid=len(seq)//2
  3.     lft, rgt = seq[:mid], seq[mid:]
  4.     if len(lft) > 1: lft = mergesort(lft)
  5.     if len(rgt) > 1: rgt = mergesort(rgt)
  6.     res=[]
  7.     while lft and rgt:
  8.         if lft[-1] >= rgt[-1]:
  9.             res.append(lft.pop())
  10.         else:
  11.             res.append(rgt.pop())
  12.     res.reverse()
  13.     return (lft or rgt) + res
  14.  
  15. a=[1,4,2,3,4,21,2,3,1]
  16.  
  17. print mergesort(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement