Quick sort within 5 lines in python
def qs(ar):
if len(ar) <= 1:
return ar
pvt = ar[len(ar) / 2]
lft = [x1 for x1 in ar if x1 < pvt]
md = [x1 for x1 in ar if x1 == pvt]
right1 = [x1 for x1 in ar if x1 > pvt]
return qs(lft) + md + qs(right1)
print qs([31,62,84,10,1,2,1])
No comments:
Post a Comment