class BubbleSort(object):
def __init__(self, data):
self.data = data
self.length = len(data)
def base_sort(self):
"""
拿到第一个数往后比较,碰到比它小的,与之交换位置,碰到比它大的则选择大的那个,用大的与后面的元素比较
:return:
"""
data_list = list(self.data)
for i in range(self.length - 1):
print("%s".center(50, "*") % (i + 1))
for index in range(self.length - i - 1):
print('index', index)
if data_list[index] > data_list[index + 1]:
data_list[index], data_list[index + 1] = data_list[index + 1], data_list[index]
return data_list
boo = BubbleSort([10, 29, 14, 37, 11, 8])
print(boo.base_sort())