# 冒泡法排序
list1 = [4,9,11,2,5,3,27]
print('------------first-------------')
list1 = [4,9,11,2,5,3,27]
for i in range(len(list1)):
for m in range(len(list1)-1):
if list1[m] > list1[m+1]:
list1[m],list1[m + 1] = list1[m + 1],list1[m]
else:
pass
print(list1)
print('--------first老师优良版--------')
list2 = [1,3,7,2,5,8]
for i in range(len(list2)-1):
for m in range(len(list2)-1-i):
if list2[m] > list2[m+1]:
list2[m],list2[m+1] = list2[m+1],list2[m]
print(list2)
below 插入
print('-----------second--------------')
def bubble(li,flag = True):
for i in range(len(li)):
for m in range(len(li)-1):
if (li[m] > li[m+1] if flag else li[m] < li[m+1]):
li[m], li[m + 1] = li[m + 1],li[m]
else:
pass
print(li)
bubble(list1,False)
print('----------third------------')
def bubble2(li2,flag=False):
li3 = []
for i in li2:
for m,n in enumerate(li3):
if (i < n if flag else i > n):
li3.insert(m,i)
break
else:
li3.append(i)
print(li3)
bubble2(list1)
print('----------forth-------------')
count = 0
li = []
while True:
num = input("Please enter a number(Exit by n):")
if num.isdigit():
count += 1
num = int(num)
while len(li) < count:
for m,n in enumerate(li):
if num > n:
li.insert(m,num)
break
else:
li.append(num)
print(li)
else:
if num == 'n':
break
else:
print('Invalid input!')