# coding=utf8 # Autor : Alaways V # Time : 2018/11/27 13:59 # File : dichotomy.py # Software PyCharm # 二分法 src_list = [] for i in range(1000): src_list.append(i) point_value = 88 def up_dichotomy(src_list): left, right = 0, len(src_list) while left < right: mid = (left + right) //2 if src_list[mid] > point_value: right = mid elif src_list[mid] < point_value: left = mid + 1 else: return '待查元素{0}在列表中下标为:{1}==={2}'.format(point_value, mid, src_list[mid]) pos = up_dichotomy(src_list) print(pos) # 冒泡排序法 src_list = [1,4,5,2,0,8,6,9] def bubble_sort(src): data_len = len(src_list) for i in range(data_len): for o in range(i+1, data_len): if src[i] > src[o]: mid_value = src[i] src[i] = src[o] src[o] = mid_value print("-----冒泡后升序列表为",src) bubble_sort(src_list)
运行结果