defquick_sort(qlist): if qlist == []: return [] else: qfirst = qlist[0] qless = quick_sort([l for l in qlist[1:] if l < qfirst]) qmore = quick_sort([m for m in qlist[1:] if m >= qfirst]) return qless + [qfirst] + qmore
defselect_sort(slist): for i inrange(len(slist)): x = i for j inrange(i, len(slist)): if slist[j] < slist[x]: x = j slist[i], slist[x] = slist[x], slist[i] return slist
slist = select_sort([4,5,6,7,3,2,6,9,8])
归并排序
归并排序:采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并