• 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]

  • 相关阅读:
    【项目管理】WBS工作分解结构
    【代理篇】内网穿透利器——Termite
    【漏洞分析】Discuz! X系列全版本后台SQL注入漏洞
    【代理篇】内网穿透利器——EarthWorm
    【漏洞复现】Apache Solr远程代码执行(CVE-2019-0193)
    【漏洞复现】Apache Solr via Velocity template远程代码执行
    【漏洞复现】PHPStudy后门
    记一次织梦cms渗透测试
    Mybatis源码手记-从缓存体系看责任链派发模式与循环依赖企业级实践
    Dubbo源码手记-服务注册
  • 原文地址:https://www.cnblogs.com/gengcx/p/6798950.html
Copyright © 2020-2023  润新知