Python实现冒泡排序、选择排序和插入排序
程序员文章站
2022-06-11 19:20:12
...
冒泡排序
def bubSort(arr):
length = len(arr)
for i in range(length):
for j in range(length-1-i):
if arr[j] > arr[j+1]:
temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
选择排序
def selectSort(arr):
length = len(arr)
for outer in range(length-1):
minner = outer
for inner in range(outer,length):
if arr[inner]<arr[minner]:
minner = inner
temp = arr[outer]
arr[outer] = arr[minner]
arr[minner] = temp
插入排序
def insertSort(arr):
length = len(arr)
for outer in range(1,length):
inner = outer
temp = arr[inner]
while(inner>=1):
if arr[inner-1] > temp:
arr[inner] = arr[inner-1]
inner -= 1
else:
break
arr[inner] = temp
下一篇: 就写黑狗吧