#!
# coding: utf-8
def dpsort(lst):
lst_len = len(lst)
if lst_len < 2:
return lst
elif lst_len == 2:
if lst[0] > lst[1]:
lst[0], lst[1] = lst[1], lst[0]
return lst
headptr = 0
for i in xrange(1, lst_len):
if lst[headptr] > lst[i]:
lst[i], lst[headptr] = lst[headptr], lst[i]
tailptr = lst_len - 1
for i in xrange(tailptr - 1, 0, -1):
if lst[tailptr] < lst[i]:
lst[i], lst[tailptr] = lst[tailptr], lst[i]
if headptr < tailptr - 2:
lst[1:-1] = dpsort(lst[1:-1])
return lst
def tester():
print dpsort([32, 12, 45, 53, 34, 97, 73, 25])
print dpsort([32, 12, 45, 53, 34, 97, 73])
print dpsort([32, 12, 45])
print dpsort([32, 12])
print dpsort([32])
print dpsort([])
if __name__ == "__main__":
tester()
只是一个样例,再自己改一改吧