defdlist_sort(dlist): if dlist == []: return [] else: dlist_lenght = len(dlist) for i inrange (0, dlist_lenght): for j inrange (i+1, dlist_lenght): if dlist[i] > dlist[j]: dlist[i],dlist[j] = dlist[j],dlist[i] return dlist
print(dlist_sort([1,5,6,3,2,4,7,8,53]))
快速排序
原理: 递归思想, 列表中取出第一个元素作为标准,比第一个小的放左侧,比第一个大的放右侧
复杂度: 时间复杂度O(nlog2n)
1 2 3 4 5 6 7 8 9 10 11
defquick_sort(quick_list): if quick_list == []: return [] else: first = quick_list[0] less = quick_sort([l for l in quick_list[1:] if l < first]) more = quick_sort([i for i in quick_list[1:] if i >= first]) return less + [first] + more