class SortSuanfa(object):
def __init__(self, data_list):
self.data_list = data_list
def num_exchange(self, i, offset_left, offset_right):
"""找到当前下标在offset的数值最合适的位置"""
mid_index = (offset_right + offset_left) // 2
flag = offset_left == mid_index
if self.data_list[i] == self.data_list[mid_index]:
self.data_list[mid_index+2:i+1], self.data_list[mid_index+1] = self.data_list[mid_index+1:i], self.data_list[i]
return
elif self.data_list[i] < self.data_list[mid_index]:
if flag:
self.data_list[mid_index+1:i+1], self.data_list[mid_index] = self.data_list[mid_index:i], self.data_list[i] # 需要特殊处理
return
self.num_exchange(i, offset_left, mid_index)
elif self.data_list[i] > self.data_list[mid_index]:
if flag:
self.data_list[mid_index+2:i+1], self.data_list[mid_index+1] = self.data_list[mid_index+1:i], self.data_list[i]
return
self.num_exchange(i, mid_index, offset_right)
def sort_list(self):
for i in range(1, len(self.data_list)):
offset = i-1
# print(f"下标为{i}, offset为{offset}, 列表为{self.data_list}")
# print(self.data_list[i])
num = self.data_list[i]
if num < self.data_list[offset]:
self.num_exchange(i, 0, offset)
if __name__ == '__main__':
# aa = [random.randint(0, 100000) for i in range(50000)]
aa = [i for i in range(200)]
aa.append(-1.5)
dd = SortSuanfa(aa)
print(aa)
print("~~~~~~~~~~~~~~~~~~~~~~~华华丽丽的分割线~~~~~~~~~~~~~~~~~~~")
dd.sort_list()
print(dd.data_list)
自己写的排序算法(亲测属于稳定排序)
于 2022-12-13 15:49:30 首次发布