初學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()