我是靠谱客的博主 自觉斑马,最近开发中收集的这篇文章主要介绍20180927_Python练习题-五:经典冒泡算法:利用for循环,完成a = [1, 7, 4, 89, 34, 2, 55, 32, 23, 10]的冒泡排序。,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
# 题目五:经典冒泡算法:利用for循环,完成a = [1, 7, 4, 89, 34, 2, 55, 32, 23, 10]的冒泡排序。
# 冒泡排序:小的排前面,大的排后面。排序最终使得数组中的这几个数字按照从小到大的顺序排序
# 冒泡的概念:1、相邻的两个元素,依次进行比较;
#
2、一般最多比较n-1趟,n是元素的个数。
a = [1, 7, 4, 89, 34, 2, 55, 32, 23, 10]
for i in range(len(a)-1):
print("第{0}次冒泡:".format(i + 1))
for j in range(len(a) - 1 - i):
if a[j] > a[j + 1]:
# change = a[j]
# a[j] = a[j + 1]
# a[j + 1] = change
a[j], a[j + 1] = a[j + 1], a[j]
# Python中实现两个数的互换
print("i={0},j={1}
{2}".format(i, j, a))
else:
print("i={0},j={1}
{2}".format(i, j, a))
continue
print("冒泡数字为:{0}".format(a[len(a)-1-i]) + "n")
print("最后排序结果:{0}".format(a))
# 第1次冒泡:
# i=0,j=0
[1, 7, 4, 89, 34, 2, 55, 32, 23, 10]
# i=0,j=1
[1, 4, 7, 89, 34, 2, 55, 32, 23, 10]
# i=0,j=2
[1, 4, 7, 89, 34, 2, 55, 32, 23, 10]
# i=0,j=3
[1, 4, 7, 34, 89, 2, 55, 32, 23, 10]
# i=0,j=4
[1, 4, 7, 34, 2, 89, 55, 32, 23, 10]
# i=0,j=5
[1, 4, 7, 34, 2, 55, 89, 32, 23, 10]
# i=0,j=6
[1, 4, 7, 34, 2, 55, 32, 89, 23, 10]
# i=0,j=7
[1, 4, 7, 34, 2, 55, 32, 23, 89, 10]
# i=0,j=8
[1, 4, 7, 34, 2, 55, 32, 23, 10, 89]
# 冒泡数字为:89
#
# 第2次冒泡:
# i=1,j=0
[1, 4, 7, 34, 2, 55, 32, 23, 10, 89]
# i=1,j=1
[1, 4, 7, 34, 2, 55, 32, 23, 10, 89]
# i=1,j=2
[1, 4, 7, 34, 2, 55, 32, 23, 10, 89]
# i=1,j=3
[1, 4, 7, 2, 34, 55, 32, 23, 10, 89]
# i=1,j=4
[1, 4, 7, 2, 34, 55, 32, 23, 10, 89]
# i=1,j=5
[1, 4, 7, 2, 34, 32, 55, 23, 10, 89]
# i=1,j=6
[1, 4, 7, 2, 34, 32, 23, 55, 10, 89]
# i=1,j=7
[1, 4, 7, 2, 34, 32, 23, 10, 55, 89]
# 冒泡数字为:55
#
# 第3次冒泡:
# i=2,j=0
[1, 4, 7, 2, 34, 32, 23, 10, 55, 89]
# i=2,j=1
[1, 4, 7, 2, 34, 32, 23, 10, 55, 89]
# i=2,j=2
[1, 4, 2, 7, 34, 32, 23, 10, 55, 89]
# i=2,j=3
[1, 4, 2, 7, 34, 32, 23, 10, 55, 89]
# i=2,j=4
[1, 4, 2, 7, 32, 34, 23, 10, 55, 89]
# i=2,j=5
[1, 4, 2, 7, 32, 23, 34, 10, 55, 89]
# i=2,j=6
[1, 4, 2, 7, 32, 23, 10, 34, 55, 89]
# 冒泡数字为:34
#
# 第4次冒泡:
# i=3,j=0
[1, 4, 2, 7, 32, 23, 10, 34, 55, 89]
# i=3,j=1
[1, 2, 4, 7, 32, 23, 10, 34, 55, 89]
# i=3,j=2
[1, 2, 4, 7, 32, 23, 10, 34, 55, 89]
# i=3,j=3
[1, 2, 4, 7, 32, 23, 10, 34, 55, 89]
# i=3,j=4
[1, 2, 4, 7, 23, 32, 10, 34, 55, 89]
# i=3,j=5
[1, 2, 4, 7, 23, 10, 32, 34, 55, 89]
# 冒泡数字为:32
#
# 第5次冒泡:
# i=4,j=0
[1, 2, 4, 7, 23, 10, 32, 34, 55, 89]
# i=4,j=1
[1, 2, 4, 7, 23, 10, 32, 34, 55, 89]
# i=4,j=2
[1, 2, 4, 7, 23, 10, 32, 34, 55, 89]
# i=4,j=3
[1, 2, 4, 7, 23, 10, 32, 34, 55, 89]
# i=4,j=4
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# 冒泡数字为:23
#
# 第6次冒泡:
# i=5,j=0
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# i=5,j=1
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# i=5,j=2
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# i=5,j=3
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# 冒泡数字为:10
#
# 第7次冒泡:
# i=6,j=0
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# i=6,j=1
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# i=6,j=2
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# 冒泡数字为:7
#
# 第8次冒泡:
# i=7,j=0
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# i=7,j=1
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# 冒泡数字为:4
#
# 第9次冒泡:
# i=8,j=0
[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
# 冒泡数字为:2
#
# 最后排序结果:[1, 2, 4, 7, 10, 23, 32, 34, 55, 89]
最后
以上就是自觉斑马为你收集整理的20180927_Python练习题-五:经典冒泡算法:利用for循环,完成a = [1, 7, 4, 89, 34, 2, 55, 32, 23, 10]的冒泡排序。的全部内容,希望文章能够帮你解决20180927_Python练习题-五:经典冒泡算法:利用for循环,完成a = [1, 7, 4, 89, 34, 2, 55, 32, 23, 10]的冒泡排序。所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复