1. Bubble method (Direct exchange Element)
1 defBubble_sort (a_list):2A_len = Len (a_list)-13 whileA_len >0:4 forIinchRange (A_len):5 ifA_list[i] >a_list[i+1]:6A_LIST[I],A_LIST[I+1] = a_list[i+1],a_list[i]7 8A_len-=19 Ten if __name__=='__main__': Onea_list=[20,40,30,90,50,80,70,60,110,100] A Bubble_sort (a_list) - Print(a_list)
2, select the sorting method (swap element position in the table, select the largest after Exchange elements)
1 defSelection_sort (a_list):2Alen =Len (a_list)3 whileAlen >0:4Postion =05 forIinchRange (1, Alen):6 ifA_list[i] >A_list[postion]:7Postion = i#Select the maximum element position subscript8A_list[postion],a_list[alen-1] = a_list[alen-1],a_list[postion]9Alen-= 1Ten One if __name__=='__main__': Aa_list=[20,40,30,90,50,80,70,60,110,100] - Selection_sort (a_list) - Print(a_list)
3. Insert sorting to insert the data into the sorted sequence. A stable sorting method.
1 defInsert_sort (alist):2Length =Len (alist)3 forIndexinchRange (1, length):4p =Index5 whilep >0:6 ifALIST[P] < Alist[p-1]:7ALIST[P],ALIST[P-1] = alist[p-1],alist[p]8P-= 19 returnAlist
Python sorting algorithm