排序
插入排序
def insert_sort(li):
for i in range(1,len(li)):
j = i - 1
tmp = li[i]
while j>=0 and li[j]>tmp:
li[j+1] = li[j]
j -= 1
li[j1] = tmp
return li
选择排序
def select(A):
A[]
for i in range(len(A)):
min_idx = i
for j in range(i + 1,len(A)):
if A[min_idx] > A[j]:
min_idx = j
A[i],A[min_idx] = A[min_idx],A[i]
for i in range(len(A)):
print("%d"%A[i])
冒泡排序
def bubblesort(arr):
n = len(arr)
for i in range(n):
for j in range(0,n-i-1):
if arr[j] > arr[j+1]:
arr[j],arr[j+1] = arr[j+1],arr[j]
arr = [64,34,25,12,22,11,90]
bubblesort(arr)
print(arr)
for i in range(len(arr)):
print("%d"%arr[i])
桶排序
def bucket_sort(li):
dic = [0] * (max(li) + 1)
for i in li:
dic[i] += 1
sort_num = []
for j in range(len(dic)):
if dic[j] != 0:
for a in range(dic[j]):
sort_num.append(j)
return sort_num
注意:
1.以上代码都带有封装函数(def 函数)的函数
2.如有不当之处请指正,不喜勿喷