目录
选择排序
冒泡排序
插入排序
计数排序
arr = [8,3,2,6,1,4,9,7]
for i in range(0,len(arr)):
for j in range(i+1,len(arr)):
if arr[i] >= arr[j]:
arr[i],arr[j] = arr[j],arr[i]
print(arr)
arr = [8,3,2,6,1,4,9,7]
for i in range(0,len(arr)-1):
for j in range(0,len(arr)-1-i):
if arr[j] >= arr[j+1]:
arr[j],arr[j+1] = arr[j+1],arr[j]
print(arr)
arr = [8,3,2,6,1,4,9,7]
for i in range(1,len(arr)):
for j in range(i,0,-1):
if arr[j] <= arr[j-1]:
arr[j],arr[j-1] = arr[j-1],arr[j]
print(arr)
arr = [7,3,2,0,1,2,3,6]
max_num = arr[0]
min_num = arr[0]
for num in arr:
if num > max_num:
max_num = num
elif num < min_num:
min_num = num
# print(max_num)
# print(min_num)
#计数列表的长度
len_arr1 = max_num - min_num + 1
#偏移量
offset = min_num
#初始化计数列表,元素全为0
arr1 = [0] * len_arr1
#排序后的列表
arr2 = [0] * len(arr)
#计数
for num in arr: # 7,3,2,0,1,2,3,6
print(num)
arr1[num-offset] += 1 # 0 0 0 0 0 0 0
print(arr1)
index = 0
for i in range(0,len_arr1):
for j in range(0,arr1[i]):
print(i+offset,end=" ")
arr2[index] = i + offset
index += 1
print()
print(arr2)