排序算法
# def bubble_sort(list):
# n = len(list)
# # 循环遍历,外层循环每循环一次,内层循环循环一遍---确定一个最大值
# for i in range(n-1):
# # 遍历无序序列,找到当前查询范围中最大的数值
# for j in range(n-1-i):
# # 判断当前节点是否大于后续节点,如果大于后续节点则对调
# if list[j] > list[j+1]:
# list[j], list[j+1] = list[j+1], list[j]
#
# if __name__ == '__main__':
# list = [22,3,44,5,66,0,99,88,7,69]
# bubble_sort(list)
# print(list)
# def bubble_sort(list):
# n = len(list)
# # 利用外层循环循环一次,内层循环循环一遍的特性, 作用:每循环一次找出一个最大值
# for i in range(n-1):
# # 循环比较相邻两个数大小,
# for j in range(n-1-i):
# # 如果前面数大小大于后面数,则交换变量的值
# if list[j] > list[j+1]:
# # 交换变量的值
# list[j],list[j+1] = list[j+1],list[j]
#
# if __name__ == '__main__':
# alsit = [99,65,888,77,66,441,2,3,4]
# bubble_sort(alsit)
# print(alsit)
# def select_sort(list):
# n = len(list)
# for i in range(n-1):
# # 给min_index 设置默认值
# min_index = i
# # 遍历无序序列
# for j in range(i+1,n):
# # 判断当前数值是否小于最小值,如果小于,则记录其索引
# if list[j] < list[min_index]:
# # 记录其索引
# min_index = j
#
# # 判断min_index索引是否相同,不相同,做数值交换
# if i != min_index:
# # 不相同,做数值交换
# list[i],list[min_index] = list[min_index],list[i]
#
# if __name__ == '__main__':
# list = [1,22,3,44,5,66,7,88,99]
# select_sort(list)
# print(list)
def select_sort(list):
n = len(list)
for i in range(n-1):
# 给min_index
# 设置默认值
min_index = i
# 遍历无序序列,去比较 i+1--外层循环1次确定一个最小值,--i+1
for j in range(i+1,n):
# 判断当前数值是否小于最小值,如果小于,则记录其索引
if list[min_index] > list[j]:
# 记录其索引
min_index = j
# 判断min_index索引是否相同,不相同,做数值交换
if min_index != i:
# 做数值交换
list[min_index],list[i] = list[i],list[min_index]
if __name__ == '__main__':
alist = [1,22,3,44444444,5,66,7,88,99]
select_sort(alist)
print(alist)