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,老规矩 ...
随机推荐
- Xml中SelectSingleNode方法,xpath查找某节点用法
Xml中SelectSingleNode方法,xpath查找某节点用法 最常见的XML数据类型有:Element, Attribute,Comment, Text. Element, 指形如<N ...
- POJ--3461
原题链接:http://poj.org/problem?id=3461 分析:求一个串在另一个串中出现的次数,显然用KMP可以解决. #include<cstdio> #include&l ...
- springMVC参数绑定与数据回显
简单例子:修改商品信息的jsp页面: 参数绑定过程: 1.2.1 默认支持的参数类型 处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值. 1.1.1 HttpServletReq ...
- phpmyadmin 登录时间修改
登录后1440秒未活动后总是自动退出,一天还要登录多次,终于有时间来解决这个问题了,感觉是session超时,结果在网上search了下,找到解决办法啦,哈哈哈,在此做个笔记:phpmyadmin在使 ...
- 为VSCODE添加右键菜单
参考:https://blog.csdn.net/GreekMrzzJ/article/details/82194913 1.创建一个名为vscode.reg的空文本文件,填入下列内容 Windows ...
- Python学习笔记(四十六)网络编程(2)— UDP编程
摘抄:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014320049779 ...
- Map总结
Map是键值对集合,是一对一对往上存的,要保持键的唯一性 形式:Map<K, V> 方法: 增 put(K key, V value) 若存储时Map中有相同的键,则返回原来键的值,并覆盖 ...
- 【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集
[题目]D. Best Edge Weight [题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1&l ...
- 使用Docker 快速搭建nuget本地服务器,Hosting private nuget server using docker in seconds!
Server #below line automatically creates the folder, mount the volumes and maps the ports. docker ru ...
- js获得页面鼠标位置
1.客户区坐标位置:clientX,clientY 鼠标相对于在当前页面可视范围左上角的位置 2.页面坐标位置:pageX,pageY 鼠标相对于页面左上角的位置(受滑动等影响,例如pageY=cli ...