算法导论 116页 ,8-7 ,突然想到一点改进
# Hello World program in Python
print "Hello World!\n"
def comp(A,i,j):
print 'baba'
if A[i]>A[j]:
A[i],A[j]=A[j],A[i]
def insertt(A):
for j in range(1,len(A)):
#for j in range(i, len(alist), 1):
for i in range(j-1, 0, -1):
#youhua
if A[i]<=A[i+1]:
print 'less time'
break
#else:
comp(A,i,i+1)
#A[i],A[i+1]=A[i+1],A[i]
aa =[1,77,81,3,4,45,67,89,99,56,44,33]
insertt(aa)
print aa
注意,break 上面的三行,能减少不必要的循环,但是,要知道,这还不如,算法导论第10页的代码。
---修改以后的代码,就是画蛇添足,所以,一定要小心。--2018-6-15
$python main.py
Hello World!
less time
baba
baba
baba
baba
less time
baba
baba
less time
baba
baba
less time
less time
less time
baba
baba
baba
baba
baba
less time
baba
baba
baba
baba
baba
baba
baba
less time
baba
baba
baba
baba
baba
baba
baba
baba
less time
[1, 3, 4, 33, 44, 45, 56, 67, 77, 81, 89, 99]
网友评论