li = [3, 4, 2, 9, 1, 7, 6, 5, 8] def bubble_sort(data): for i in range(len(data)-1): exchange = True for j in range(len(data)-i-1): if data[j] > data[j+1]: data[j], data[j+1] = data[j+1], data[j] exchange = False if exchange: return print(data) if __name__ == '__main__': bubble_sort(li)
返回: