- #!/usr/bin/python
- # coding:utf8
- # 冒泡排序
- def sort1(list1):
- for i in range(len(list1)):
- isEnd = True
- for j in range(len(list1)-1,0,-1):
- if list1[j]<list1[j-1]:
- tmp = list1[j]
- list1[j] = list1[j-1]
- list1[j-1] = tmp
- isEnd = False
- if isEnd:
- break
- list1 = [5,3,23,67,2,56,45,98,239,9]
- sort1(list1)
- print list1
运行结果:
[2, 3, 5, 9, 23, 45, 56, 67, 98, 239]
来源: http://www.bubuko.com/infodetail-2615751.html