排序原理
1、 将所有元素分组, 已排序和未排序的
2、 遍历未排序的数组,并倒序遍历已排序的数组进行比较,如果比已排序的小,就插入
class Insert:
def __init__(self):
super(Insert, self).__init__()
@staticmethod
def sort(a):
# 顺序遍历未排序的数组
for i in range(1, len(a)):
# 倒序遍历已排序的数组
for j in range(i-1, -1, -1):
if Insert.compare(a, j, i):
Insert.exchange(a, j, i)
# 交换索引位置
i = j
@staticmethod
def compare(a, i, j):
if a[i] >= a[j]:
return True
return False
@staticmethod
def exchange(a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp
if name == ‘main’:
insert = Insert()
t = [1, 3, 2, 4, 0, 6, 5, 7, 123, 1, 25, 3, 123, 5, 5, 2, 3, 5, 9, 12, 34, 231, 32, 12, 23]
insert.sort(t)
print(t)