#encoding=utf-8 import random from copy import copy def directInsertSort(seq): """ 直接插入排序 """ size = len(seq) for i in range(1,size): tmp, j = seq[i], i while j > 0 and tmp < seq[j-1]: seq[j], j = seq[j-1], j-1 seq[j] =
lst = [2,22,4,7,18]for j in range(len(lst)): #记录内部排序的次数 i = 0 while i < len(lst)-1: if lst[i] > lst[i+1]: # 判断前后的大小 lst[i],lst[i+1] = lst[i+1],lst[i] #最大的往前右排,互换位子 i = i + 1print(lst)
一.列表排序 # python中对列表排序有sort.sorted两种方法,其中sort是列表内置方法,其帮助文档如下:In [1]: help(sorted) Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascendi