刚刚学习 Python 并开始学习排序列表的主题。展示了两种类型的算法:插入和选择。所以,我有一个想法并创建了这个:
def DiffSort(lst):
lstDiff = [None] * len(lst)
i = 0
while i < len(lst):
lstDiff[i] = lst[i] - lst[i-1] if i != 0 else lst[0]
if lstDiff[i] < 0:
sbj, tmp = lst[i], lstDiff[i]
while tmp < 0:
i -= 1
tmp += lstDiff[i]
lst[i+1] = lst[i]
lst[i] = sbj
else:
i += 1
lst = [13,25,18,122,32,1,0.78,25,85,1,32,56,0.55,0.6,17]
print(lst)
DiffSort(lst)
print(lst)
有什么好处吗?是否已经有类似的方法?