初学Python,写一些算法作为练手。输入‘e' 作为输入的结束
Python算法之插入排序代码:
def insert_sort(seq):
length = len(seq)
for i in range(1,length):
temp = seq[i]
for j in range(i-1, -1, -1):
if temp < seq[j]:
seq[j+1]=seq[j]
else:
j+=1
break
if seq[j] is not temp:
seq[j]=temp
def do_test():
seq = []
print('Please input the sequence:')
while True:
ch = input()
if ch is 'e':
break
seq.append(int(ch))
insert_sort(seq)
print('After the insertSort:')
print(seq)
if __name__ == '__main__':
do_test()