冒泡排序 def bubblesort(alist): for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp 选择排序 def selectionsort(alist): for fillslot in range(len(alist)-1,0,-1): posofmax = 0 for location in range(1,fillslot+1): if alist[location]>alist[posofmax]: posofmax = location tmp = alist[fillslot] alist[fillslot] = alist[posofmax] alist[posofmax] = tmp 插入排序 def insertsort(alist): for index in range(1,len(alist)): currentvalue = alist[index] position = index while position>0 and alist[position-1]>currentvalue: alist[position] = alist[position-1] position = position-1 alist[position] = currentvalue