Python写五种排序
1.冒泡排序:
1 | def bubble(ori_list, n): |
2.选择排序:
1 | def select(ori_list, n): |
3.插入排序:
1 | def insert(ori_list, n): |
4.归并排序:
1 | def merge(left, right): |
5.快速排序:
1 | def quick_sort(lists, left, right): |
- 简易写法:
1
2
3
4
5
6def qsort(q):
if len(q) <= 1:
return q
else:
p = q[0]
return qsort([x for x in q[1:] if x < p]) + [p] + qsort([x for x in q[1:] if x >= p])