冒泡排序过程代码解读

#!/usr/bin/env python
# -*- coding:utf-8 -*-

li = [33,2,10,1]

''' 过程
for i in range(len(li) - 1):
current = li[0]
next_value = li[1]
if li[i] > li[i+1]:
temp = li[i]
li[i] = li[i+1]
li[i+1] = temp
print(li)

for i in range(len(li) - 2):
current = li[0]
next_value = li[1]
if li[i] > li[i+1]:
temp = li[i]
li[i] = li[i+1]
li[i+1] = temp
print(li)

for i in range(len(li) - 3):
current = li[0]
next_value = li[1]
if li[i] > li[i+1]:
temp = li[i]
li[i] = li[i+1]
li[i+1] = temp
print(li)
'''

for j in range(1,len(li)): # 1,2,3
for i in range(len(li) - j): # 1,2,3
current = li[0]
next_value = li[1]
if li[i] > li[i + 1]:
temp = li[i]
li[i] = li[i + 1]
li[i + 1] = temp

print(li)
posted @ 2018-09-23 23:09  黄瓜不是好瓜  阅读(231)  评论(0编辑  收藏  举报