#!/usr/bin/python
def bubble_sort(array):
for i in range(len(array)-1,):
for j in range((i+1),len(array)):
if array[j] < array[i]:
array[j],array[i]=array[i],array[j]
print array
def main():
array=[10,6,19,3,15,2,3,8]
bubble_sort(array)
if __name__=='__main__':
main()
运行程序后,其每步排序如下图:
[6, 10, 19, 3, 15, 2, 3, 8]
[6, 10, 19, 3, 15, 2, 3, 8]
[3, 10, 19, 6, 15, 2, 3, 8]
[3, 10, 19, 6, 15, 2, 3, 8]
[2, 10, 19, 6, 15, 3, 3, 8]
[2, 10, 19, 6, 15, 3, 3, 8]
[2, 10, 19, 6, 15, 3, 3, 8]
[2, 10, 19, 6, 15, 3, 3, 8]
[2, 6, 19, 10, 15, 3, 3, 8]
[2, 6, 19, 10, 15, 3, 3, 8]
[2, 3, 19, 10, 15, 6, 3, 8]
[2, 3, 19, 10, 15, 6, 3, 8]
[2, 3, 19, 10, 15, 6, 3, 8]
[2, 3, 10, 19, 15, 6, 3, 8]
[2, 3, 10, 19, 15, 6, 3, 8]
[2, 3, 6, 19, 15, 10, 3, 8]
[2, 3, 3, 19, 15, 10, 6, 8]
[2, 3, 3, 19, 15, 10, 6, 8]
[2, 3, 3, 15, 19, 10, 6, 8]
[2, 3, 3, 10, 19, 15, 6, 8]
[2, 3, 3, 6, 19, 15, 10, 8]
[2, 3, 3, 6, 19, 15, 10, 8]
[2, 3, 3, 6, 15, 19, 10, 8]
[2, 3, 3, 6, 10, 19, 15, 8]
[2, 3, 3, 6, 8, 19, 15, 10]
[2, 3, 3, 6, 8, 15, 19, 10]
[2, 3, 3, 6, 8, 10, 19, 15]
[2, 3, 3, 6, 8, 10, 15, 19]