day5冒泡排序
冒泡排序:是一种基础的算法,实现数据的排序,排序的原则是前一个与后一个进行比较,如果前面的值大则交换,否则不交换,多次循环每次把最大的数据循环至后面就能够完成所需。
上面的图是冒泡排序的原理,每次循环把最大的值遍历到最后面,而且循环的过程中,每次循环的次数减1。
下面来看一个实例,我们将一个列表data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6],我们将列表进行排序。代码如下:
首先取出列表的顺序和下表enumerate(
data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6]
for index,i in enumerate(data[0:-1]):
if i > data[index+1]:
data[index+1],data[index] = i,data[index+1]
print(data)
运行如下:
[4, 10, 21, 33, 3, 54, 8, 5, 11, 2, 1, 2, 13, 6, 13]
可以看出是调换了位置,但是是有错误的。13出现了两次,22消失了,这是由于在循环过程中,我们改动了列表的下标,改动下标之后就出现了错误,有些值被替换了。因为不能这样做,或者下面这样也是不行的。
data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6]
for index,i in enumerate(data[0:-1]):
if i > data[index+1]:
#data[index+1],data[index] = i,data[index+1]
#存储一个临时变量用来存储信息
tmp = data[index+1]
data[index+1] = i
data[index] = tmp
print(data)
上面代码中tem的作用是存储一个data[index+1]因为我们知道,要把data[index]和data[index+1]进行交换,如果单纯的交换,有一个会被替换,因为要把被替换的值存在一个新的变量中,这样就避免了这种错误。
data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6]
for j in range(len(data)):
for i in range(len(data)-1):
if data[i] > data[i+1]:
tem = data[i+1]
data[i+1] = data[i]
data[i] = tem
print(data)
运行代码结果如下:
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
上面代码中,我们的思路是循环列表中的每个元素,前一个和后一个进行比较,如果前一个大于后一个就进行替换,否则不动,这样每次把最后一个最大值移到列表的末尾,就能实现功能,列表有多少长度,我们就循环多少次,因此我们在外面加入了for循环,让下面的列表排序能够进行多次比较。但是上面的代码可以有一个改进的地方,我们知道,每次循环都会把一个最大值放到列表的末尾,因此循环的次数可以每次递减1次,下面我们对代码进行改进:
data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6]
for j in range(1,len(data)):
for i in range(len(data)-j):
if data[i] > data[i+1]:
tem = data[i+1]
data[i+1] = data[i]
data[i] = tem
print(data)
运行结果如下:
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
结果是一样的,但是我们这里避免了很多不必要的循环,我们看一下两个代码各自需要循环多少次,为此我们可以定义一个n在里面,每次循环的时候加1。
data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6]
n = 0
for j in range(len(data)):
#for j in range(1,len(data)):
for i in range(len(data)-1):
#for i in range(len(data) - j):
n = n + 1
if data[i] > data[i+1]:
tem = data[i+1]
data[i+1] = data[i]
data[i] = tem
print(n)
print(data)
经过上面两种方法的测试,没有改进的方法运行了210次,改进之后的方法值运行了105次,可见很多时候改进一点点就能节省很多时间。
下面来看看代码每次运行的结果:
[4, 10, 33, 21, 54, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 54, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 54, 8, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 54, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 54, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 54, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 54, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 54, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 54, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 54, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 54, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 33, 8, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 33, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 33, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 33, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 33, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 33, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 33, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 33, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 33, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 21, 8, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 21, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 21, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 22, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 22, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 22, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 22, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 10, 8, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 21, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 21, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 21, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 21, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 10, 5, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 11, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 13, 17, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 10, 2, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 10, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 1, 10, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 1, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 8, 1, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 1, 8, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 1, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 5, 1, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 1, 5, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 1, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 4, 1, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 1, 4, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 1, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[2, 3, 1, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[2, 1, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
上面是分布每次运行的情况,下面是没一大步运行的情况:
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 1, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 1, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 1, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 1, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[2, 1, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
当然还有一种简单的方法能够实现列表的排序,而且只需要循环列表长度的次数即可:
data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6]
numbers = []
#查找列表中最小值的位置
for i in range(len(data)):
num = data.pop(data.index(min(data)))
numbers.append(num)
print(numbers)
方法的原理是,我们知道,目的是实现列表中元素的排序,那么我们每次找到列表中的最小值并且把这个最小值使用pop方法弹出来,那么列表的长度每次减1,我们每次都只找最小值,使用index()查找值的索引,使用另外一个列表去接收。就能够实现,运行结果如下:
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
day5冒泡排序的更多相关文章
- python day5 lambda,内置函数,文件操作,冒泡排序以及装饰器
目录 python day 5 1. 匿名函数lambda 2. python的内置函数 3. python文件操作 4. 递归函数 5. 冒泡排序 6. 装饰器 python day 5 2019/ ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)
本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...
- Html5 冒泡排序演示
冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要 ...
- javascript冒泡排序
数组冒泡排序算法(升序) 升序:小数在前,大数在后 冒泡排序的原则:每次比较相邻两个元素,如果前一个数>后一个数,说明违反升序的要求,就将两数交换位置.否则,保持不变.继续比较下一对. 例如:玩 ...
- Java中的经典算法之冒泡排序(Bubble Sort)
Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...
- C#冒泡排序算法
用了两种形式的数据,一个是泛型List,一个是数据int[].记录一下,作为自己学习过程中的笔记. using System; using System.Collections.Generic; us ...
- python排序之二冒泡排序法
python排序之二冒泡排序法 如果你理解之前的插入排序法那冒泡排序法就很容易理解,冒泡排序是两个两个以向后位移的方式比较大小在互换的过程好了不多了先上代码吧如下: 首先还是一个无序列表lis,老规矩 ...
随机推荐
- php curl使用ss代理
1.安装 ss,过程略 2.ss 配置文件 { "server":"x.x.x.x", #你的 ss 服务器 ip "server_port" ...
- 手脱Upack 2.x - 3.x
1.PEID查壳 Upack 2.x - 3.x Heuristic Mode -> Dwing 2.载入OD,一上来就是一个大跳转,先F8跟一会 >- E9 56D40300 jmp 跑 ...
- ASP.NET 使用ajaxupload.js插件出现上传较大文件失败的解决方法
在网上下载了一个ajaxupload.js插件,用于无刷新上传图片使的,然后就按照demo的例子去运行了一下,上传啊什么的都OK,但是正好上传的示例图片有一个比较大的,4M,5M的样子,然后上传就会报 ...
- dp+分类讨论 Gym 101128E
题目链接:http://codeforces.com/gym/101128 感觉这个人写的不错的(我只看了题目大意):http://blog.csdn.net/v5zsq/article/detail ...
- spfa+剪枝 或者 dij+手写堆+剪枝 UOJ 111
http://uoj.ac/problem/111 好像NOIP里面的题目...有好多都是...能通过xjbg剪枝来...AC题目的? 得好好学一下这些剪枝黑科技了... 思路:我觉得这位大佬说的很完 ...
- 重构改善既有代码设计--重构手法18:Self Encapsulate Field (自封装字段)
你直接访问一个值域(field),但与值域之间的耦合关系逐渐变得笨拙. 为这个值域建立取值/设值函数(getting/setting methods),并且只以这些函数来访问值域. private i ...
- CSS3的新属性
1.圆角矩形 .border_radius_test{ border-radius:25px; -moz-border-radius:25px; } 数值越大越圆 2.容器阴影 .box_shadow ...
- 朋友封装的一个ASP.NET上传文件的方法
朋友做了asp.net开发多年,做了这个,自我感觉封装得还不错!!! 代码如下: #region 上传文件的方法 /// <summary> /// 上传文件方法 /// </sum ...
- Linux 安装tomcat,搭建web app运行环境
Tomcat 8 下载地址:https://tomcat.apache.org/download-80.cgi 解压tomcat:tar -xf apache-tomcat-8.5.31.tar.gz ...
- ajax做显示信息以后用ajax、Bootstrp做弹窗显示信息详情
1.用ajax做弹窗显示信息详情 nation.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...