Python3 中如何实现冒泡排序算法?

什么是冒泡排序算法?

临近的两个元素进行比较,然后互换顺序,一趟走完之后最大的或者最小的元素排在第一个或者最后一个位置(最大或者最小看实际需求),给的的感觉就是一个小泡泡一直冒啊冒,所以有个好听的名字叫冒泡算法

举个例子,有数组[10, 1, 18, 30, 23, 12, 7, 5, 18, 17],我们使用从小到大的排序方法

第一趟,外排下标为0,对应的值为10

内排开始

①下标0和下标1的值进行比较。10和1比,发现1比10小,进行互换,内排一次完成,结果是[1, 10, 18, 30, 23, 12, 7, 5, 18, 17]

②下标1和下标2的值进行比较。10和18比,10比18小,保持不变,结果是[1, 10, 18, 30, 23, 12, 7, 5, 18, 17]

③下标2和下标3的值进行比较。18和30比,18比30小,保持不变,结果是[1, 10, 18, 30, 23, 12, 7, 5, 18, 17]

④下标3和下标4的值进行比较。30和23比,30比23大,进行互换,结果是[1, 10, 18, 23, 30, 12, 7, 5, 18, 17]

……一直这样紧邻的两个数比下去,第一趟外排结束,结果是[1, 10, 18, 23, 12, 7, 5, 18, 17, 30],我们发现,我们经过了这一次排序,把最大值30送到了右侧

第二趟,外排下标是1

内排开始

①下标0和下标1的值进行比较。1和10比,1比10小,保持不变,内排一次完成,结果是[1, 10, 18, 23, 12, 7, 5, 18, 17, 30]

②下标1和下标2的值进行比较。10和18比,10比18小,保持不变,内排二次完成,结果是[1, 10, 18, 23, 12, 7, 5, 18, 17, 30]

…………

④下标3和下标4的值进行比较。12和23比,23比12小,进行互换,内排四次完成,结果是[1, 10, 18, 12, 23, 7, 5, 18, 17, 30]

⑤下标4和下标5的值进行比较。23和7比较,23比7大,进行互换,内排五次完成,结果是[1, 10, 18, 12, 7, 23, 5, 18, 17, 30]

…………经过一系列排序,结束,结果为[1, 10, 18, 12, 7, 5, 18, 17, 23, 30],这个时候我们把第二大的数据23放到正确的位置

第三趟

…………结果为[1, 10, 12, 7, 5, 18, 17, 18, 23, 30]

…………

…………

最终结果为[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]

至此,排序完成。

Python3 代码实例

#!/usr/bin/python
# -*- coding: utf-8 -*-
#冒泡排序
 
def bubble_sort(the_list):
    i = 0
    while i < len(the_list):
        j = 0
        while j < len(the_list)-1:
            print(the_list[j],the_list[j+1])
            if the_list[j] > the_list[j+1]:
                the_list[j], the_list[j+1] = the_list[j+1], the_list[j]
            j = j+1
            print(the_list)
        print("======"+str(the_list))
        i = i+1
    return the_list
if __name__ == '__main__':
    the_list = [10, 1, 18, 30, 23, 12, 7, 5, 18, 17]
    print("排序前:" + str(the_list))
    print("排序后:" + str(bubble_sort(the_list)))

运行结果

排序前:[10, 1, 18, 30, 23, 12, 7, 5, 18, 17]
10 1
[1, 10, 18, 30, 23, 12, 7, 5, 18, 17]
10 18
[1, 10, 18, 30, 23, 12, 7, 5, 18, 17]
18 30
[1, 10, 18, 30, 23, 12, 7, 5, 18, 17]
30 23
[1, 10, 18, 23, 30, 12, 7, 5, 18, 17]
30 12
[1, 10, 18, 23, 12, 30, 7, 5, 18, 17]
30 7
[1, 10, 18, 23, 12, 7, 30, 5, 18, 17]
30 5
[1, 10, 18, 23, 12, 7, 5, 30, 18, 17]
30 18
[1, 10, 18, 23, 12, 7, 5, 18, 30, 17]
30 17
[1, 10, 18, 23, 12, 7, 5, 18, 17, 30]
======[1, 10, 18, 23, 12, 7, 5, 18, 17, 30]
1 10
[1, 10, 18, 23, 12, 7, 5, 18, 17, 30]
10 18
[1, 10, 18, 23, 12, 7, 5, 18, 17, 30]
18 23
[1, 10, 18, 23, 12, 7, 5, 18, 17, 30]
23 12
[1, 10, 18, 12, 23, 7, 5, 18, 17, 30]
23 7
[1, 10, 18, 12, 7, 23, 5, 18, 17, 30]
23 5
[1, 10, 18, 12, 7, 5, 23, 18, 17, 30]
23 18
[1, 10, 18, 12, 7, 5, 18, 23, 17, 30]
23 17
[1, 10, 18, 12, 7, 5, 18, 17, 23, 30]
23 30
[1, 10, 18, 12, 7, 5, 18, 17, 23, 30]
======[1, 10, 18, 12, 7, 5, 18, 17, 23, 30]
1 10
[1, 10, 18, 12, 7, 5, 18, 17, 23, 30]
10 18
[1, 10, 18, 12, 7, 5, 18, 17, 23, 30]
18 12
[1, 10, 12, 18, 7, 5, 18, 17, 23, 30]
18 7
[1, 10, 12, 7, 18, 5, 18, 17, 23, 30]
18 5
[1, 10, 12, 7, 5, 18, 18, 17, 23, 30]
18 18
[1, 10, 12, 7, 5, 18, 18, 17, 23, 30]
18 17
[1, 10, 12, 7, 5, 18, 17, 18, 23, 30]
18 23
[1, 10, 12, 7, 5, 18, 17, 18, 23, 30]
23 30
[1, 10, 12, 7, 5, 18, 17, 18, 23, 30]
======[1, 10, 12, 7, 5, 18, 17, 18, 23, 30]
1 10
[1, 10, 12, 7, 5, 18, 17, 18, 23, 30]
10 12
[1, 10, 12, 7, 5, 18, 17, 18, 23, 30]
12 7
[1, 10, 7, 12, 5, 18, 17, 18, 23, 30]
12 5
[1, 10, 7, 5, 12, 18, 17, 18, 23, 30]
12 18
[1, 10, 7, 5, 12, 18, 17, 18, 23, 30]
18 17
[1, 10, 7, 5, 12, 17, 18, 18, 23, 30]
18 18
[1, 10, 7, 5, 12, 17, 18, 18, 23, 30]
18 23
[1, 10, 7, 5, 12, 17, 18, 18, 23, 30]
23 30
[1, 10, 7, 5, 12, 17, 18, 18, 23, 30]
======[1, 10, 7, 5, 12, 17, 18, 18, 23, 30]
1 10
[1, 10, 7, 5, 12, 17, 18, 18, 23, 30]
10 7
[1, 7, 10, 5, 12, 17, 18, 18, 23, 30]
10 5
[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
10 12
[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
12 17
[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
17 18
[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
18 18
[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
18 23
[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
23 30
[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
======[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
1 7
[1, 7, 5, 10, 12, 17, 18, 18, 23, 30]
7 5
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
7 10
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
10 12
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
12 17
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
17 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 23
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
23 30
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
======[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
1 5
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
5 7
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
7 10
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
10 12
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
12 17
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
17 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 23
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
23 30
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
======[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
1 5
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
5 7
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
7 10
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
10 12
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
12 17
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
17 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 23
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
23 30
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
======[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
1 5
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
5 7
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
7 10
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
10 12
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
12 17
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
17 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 23
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
23 30
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
======[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
1 5
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
5 7
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
7 10
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
10 12
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
12 17
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
17 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 18
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
18 23
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
23 30
[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
======[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]
排序后:[1, 5, 7, 10, 12, 17, 18, 18, 23, 30]

链接到文章: https://ixvps.com/46475.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注