def sortList(alist):
alen = len(alist)
if alen == 0:
return alist
if alen > 0:
aitem = alist[alen / 2]
aleftlist = []
arightlist = []
for i in range(0,alen):
afirstitem = alist[i]
if i <> alen / 2:
if afirstitem > aitem:
arightlist.append(afirstitem)
else:
aleftlist.append(afirstitem)
aleftlist = sortList(aleftlist)
arightlist = sortList(arightlist)
aleftlist.append(aitem)
return aleftlist + arightlist
alist = [0,1,2]
alen = len(alist)
if alen == 0:
return alist
if alen > 0:
aitem = alist[alen / 2]
aleftlist = []
arightlist = []
for i in range(0,alen):
afirstitem = alist[i]
if i <> alen / 2:
if afirstitem > aitem:
arightlist.append(afirstitem)
else:
aleftlist.append(afirstitem)
aleftlist = sortList(aleftlist)
arightlist = sortList(arightlist)
aleftlist.append(aitem)
return aleftlist + arightlist
alist = [0,1,2]
print(sortList(alist))
本文转自鹅倌51CTO博客,原文链接: http://blog.51cto.com/kaixinbuliao/1742348,如需转载请自行联系原作者