当前位置: 编程技术>python
python 快速排序代码
来源: 互联网 发布时间:2014-09-04
本文导语: 代码如下: def quick_sort(ls): return [] if ls == [] else quick_sort([y for y in ls[1:] if y < ls[0]]) + [ls[0]] + quick_sort([y for y in ls[1:] if y >= ls[0]]) if __name__ == '__main__': l1 = [3,56,8,1,34,56,89,234,56,231,45,90,33,66,88,11,22] l2 = quick_sort(l1) print l1 print l2 注意:q...
代码如下:
def quick_sort(ls):
return [] if ls == [] else quick_sort([y for y in ls[1:] if y < ls[0]]) + [ls[0]] + quick_sort([y for y in ls[1:] if y >= ls[0]])
if __name__ == '__main__':
l1 = [3,56,8,1,34,56,89,234,56,231,45,90,33,66,88,11,22]
l2 = quick_sort(l1)
print l1
print l2
注意:quick_sort函数中的代码是在一行里面的