python实现冒泡排序、插入排序
程序员文章站
2022-06-11 19:20:48
...
def bubble_sort(S):
N=len(S)
for i in range(N-1): #epoch次数
for j in range(N-1-i):
if S[j]>S[j+1]:
S[j],S[j+1]=S[j+1],S[j]
return(S)
S=[2,3,5,4,1]
bubble_sort(S)
def insertion_sort(S):
N=len(S)
for i in range(N-1):
for j in range(i+1):
if S[i+1]<S[j]:
S[j],S[j+1:i+2]=S[i+1],S[j:i+1]
return(S)
insertion_sort(S)