|
编码小白,想问下为什么这次排序里面2和12明明不满足string_test[leng2]<string_test[leng2-1]条件,还是执行了while里面的代码,被翻转了呢?
****['2', '12', '23', '41', '19', '16', '56', '24', '10', '8']
1
2 12
12 2
['12', '2', '23', '41', '19', '16', '56', '24', '10', '8']
代码如下
string_test=['23','2','41','12','19','16','56','24','10','8']
len_test=len(string_test)
for leng in range(1,len_test):
if string_test[leng]<string_test[leng-1]:
leng2=leng
print '-----------------------------------'
print 'leng is'+str(leng)
while string_test[leng2]<string_test[leng2-1] and leng2>0:
print '****'+str(string_test)
print leng2
print string_test[leng2-1],string_test[leng2]
temp=string_test[leng2-1]
string_test[leng2-1]=string_test[leng2]
string_test[leng2]=temp
print string_test[leng2-1],string_test[leng2]
leng2-=1
print string_test
print string_test |
|