a = [4, 6, 2, 1, 8, 3]
for _ in a:
for i in xrange(len(a)-1):
if a[i] > a[i+1]:
tmp = a[i+1]
# a[i+1] = a[i]
# a[i] = tmp
a[i+1], a[i] = a[i], tmp
print a
a = [4, 6, 2, 1, 8, 3]
for _ in a:
for i in xrange(len(a)-1):
if a[i] > a[i+1]:
tmp = a[i+1]
# a[i+1] = a[i]
# a[i] = tmp
a[i+1], a[i] = a[i], tmp
print a
本文标题:冒泡排序
本文链接:https://www.haomeiwen.com/subject/sntjbctx.html
网友评论