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,老规矩 ...
随机推荐
- winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已解] 望一起讨论)(技术改变世界-cnblog)
http://www.cnblogs.com/IAmBetter/archive/2012/01/14/2322156.html winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已 ...
- Codeforces Round #305 (Div. 2) D 维护单调栈
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- thinkphp常见问题
1.数据库查询中execute和query方法的区别 tp中execute()和query()方法都可以在参数里直接输入sql语句. 但是不同的是execute()通常用来执行insert或者upda ...
- 对于redis底层框架的理解(一)
近期学习了redis底层框架,好多东西之前都没听说过,算是大开眼界了. 先梳理下redis正常的通讯流程吧 首先服务器启动都有主函数main,这个main函数就在redis.c里 首先是initser ...
- 题解 P3153 【[CQOI2009]跳舞】
P3153 [CQOI2009]跳舞 题目描述 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢 ...
- Mac(Linux)上安装memcached步骤
Mac上安装memcached类似于在Linux平台上安装memcached. 主要需要做两块: 一.安装libevent库: 二.安装memcached; 一.安装libevent库 libeven ...
- API图片路径和超链接语义化转换
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- HDU6127 简单几何 暴力二分
LINK 题意:给出n个点,每个点有个权值,可以和任意另外一点构成线段,值为权值积.现问过原点的直线中交所有线段的权值和的最大值,注意直线必不经过点. 思路:直线可以将点集分为两侧,此时的权值为两侧点 ...
- css单位em、px、rem和pt的区别
1.PX :像素(Pixel) PX是相对长度单位,它是相对于显示器屏幕分辨率而言的. 优缺点:比较稳定和精确,但在浏览器中放大或缩放浏览页面时会出现页面混乱的情况. 2.EM:是相对长度单位. EM ...
- laravel 重定向路由带参数
转载: http://www.cnblogs.com/foreversun/p/5642176.html 举例: 路由: //任务列表页 $router->get('/taskDetail/{i ...