abhi12ravi

merge sort.py

Aug 26th, 2014
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. def msort2(x):
  2.     if len(x) < 2:
  3.         return x
  4.     result = []          # moved!
  5.     mid = int(len(x)/2)
  6.     y = msort2(x[:mid])
  7.     z = msort2(x[mid:])
  8.     while (len(y) > 0) and (len(z) > 0):
  9.             if y[0] > z[0]:
  10.                 result.append(z[0])
  11.                 z.pop(0)
  12.             else:
  13.                 result.append(y[0])
  14.                 y.pop(0)
  15.     result += y
  16.     result += z
  17.     return result
Advertisement
Add Comment
Please, Sign In to add comment