``````def selection_sort(alist):              n   =   len(alist)              #   需要进⾏n-1次选择操作                for i   in  range(n-1):                             #   记录最⼩位置                              min_index   =   i                               #   从i+1位置到末尾选择出最⼩数据                                for j   in  range(i+1,  n):                                             if  alist[j]    <    alist[min_index]:                                                               min_index   =   j                               #   如果选择出的数据不在正确位置，进⾏交换                             if  min_index   !=  i:                                              alist[i],   alist[min_index]    =   alist[min_index],   al ist[i]
alist   =   [54,226,93,17,77,31,44,55,20]
selection_sort(alist) print(alist)``````

``````def insert_sort(alist):             #   从第⼆个位置，即下标为1的元素开始向前插⼊               for i   in  range(1,    len(alist)):                                #   从第i个元素开始向前⽐较，如果⼩于前⼀个元素，交换位置                             for j   in  range(i,    0,  -1):                                                if  alist[j]    <    alist[j-1]:
alist[j],   alist[j-1]  =   alist[j-1], alist[j]
alist   =   [54,26,93,17,77,31,44,55,20] insert_sort(alist) print(alist)``````