1 classSort (object):2 3 @staticmethod4 defbubble_sort (LS):5Lenth =len (LS)6 ifLenth = =0:7 return []8 whileLenth:9 forIinchRange (lenth-1):Ten ifLs[i] > Ls[i+1]: OneLs[i], ls[i+1] = ls[i+1], Ls[i] ALenth-= 1 - returnls - the @staticmethod - defselect_sort (LS): - if notls: - return [] +Lenth =len (LS) -i =0 + whileI < Lenth-1: AMin_v =Ls[i] at forNinchRange (i+1, lenth): - ifLs[n] <Min_v: -LOC, Min_v =N, Ls[n] - ifLs[i]! =Min_v: -Ls[i], Ls[loc] =Ls[loc], Ls[i] -i + = 1 in returnls - to @staticmethod + definsert_sort (LS): - if notls: the return [] *i = 1 $Lenth =len (LS)Panax Notoginseng whileI <Lenth: - forNinchRange (0, i): the ifLs[n] > Ls[n+1]: +Ls[n], ls[n+1] = ls[n+1], Ls[n] Ai + = 1 the returnls + - $ $ - if __name__=='__main__': -ls = [1, 9, 5, 4, 3, 7, 6] thes =Sort () - Print(S.bubble_sort (LS))Wuyi Print(S.select_sort (LS)) the Print(S.insert_sort (LS))
Know:
- Bubble sort is the maximum value (minimum value) that is exchanged to the end of the line (at the beginning of the row) by adjacent;
- Select sorting is the maximum value (minimum) to be placed at the end of the line (at the beginning);
- An insert sort is an adjacent interchange that inserts the next number into an already sequenced sequence.
Complete sorting using Python