字两两进行比较,按照从小到大或者从大到小的顺序进行交换

例:

原始数组:[3,9,4,7,8,1,0] 7个

1、[3, 4, 7, 8, 1, 0, 9]
2、[3, 4, 7, 1, 0, 8, 9]
3、[3, 4, 1, 0, 7, 8, 9]
4、[3, 1, 0, 4, 7, 8, 9]
5、[1, 0, 3, 4, 7, 8, 9]
6、[0, 1, 3, 4, 7, 8, 9]



python 版:

nn=[3,9,4,7,8,1,0]


def bubble_sort(lists):
    count = len(lists)
    for i in range(len(lists)-1):
        for j in range(len(lists)-1):
            if lists[j]>lists[j+1]:
                lists[j],lists[j+1]=lists[j+1],lists[j]
    return lists

a=bubble_sort(nn)
print(a)

nn=[3,9,4,7,8,1,0]
nn.sort()

#验证正确性
if nn==a:
  print("ok")