list1 = [1, 2, 4, 6, 6, 1, 4, 9] list1.sort() print(list1) if len(list1) % 2 == 0: print((list1[len(list1) // 2] + list1[len(list1) // 2 - 1])/2) else: print(list1[len(list1) // 2])
#运行结果
#冒泡排序
def bubble_sort(lst): length = len(lst) for j in range(length - 1): # 循环次数为列表长度-1 for i in range(length - j - 1): # 每次循环 两个位置进行比较 if lst[i] > lst[i + 1]: temp = lst[i] lst[i] = lst[i + 1] lst[i + 1] = temp print(lst) bubble_sort([11, 2, 34, 55, 6]) ''' 分析过程 边数 比较次数 j length - j -1 0 length -1 1 length -2 2 length -3 '''
#运行结果