zoukankan      html  css  js  c++  java
  • Python中怎么使用(冒泡排序)?

    一、冒泡排序

      1.   原理:比较两个相邻的元素,将值大的元素交换到右边

      2.   思路:依次比较相邻的两个数,将比较小的数放在前面,比较大的数放在后面。

        (1)第一次比较:首先比较第一和第二个数,将小数放在前面,将大数放在后面。

        (2)比较第2和第3个数,将小数 放在前面,大数放在后面。

        (3)如此继续,知道比较到最后的两个数,将小数放在前面,大数放在后面,重复步骤,直至全部排序完成

      3.   举例:要排序的数组 

        li = [1,3,5,9,70, 135, 12, 17, 24, 34]
     1 li = [1,3,5,9,70, 135, 12, 17, 24, 34]
     2 
     3 for i in range(len(li)):  # i 是固定位置的元素
     4     print("当前用来对比的位置是:%s,数据是:%s" % (i, li[i]))
     5     for j in range(len(li)):
     6         # j是用来被对比的每个元素     用的是list中每个value去循环,做数据交换 没法去做
     7         print("现在用%s和%s进行对比" % (li[i], li[j]))
     8         if li[i] < li[j]:
     9             li[i], li[j] = li[j], li[i]
    10         print(li)
    11     print("======当前位置:%s,数据:%s 已结束==========" % (i, li[i]))
    12 
    13 print(li)

    运行代码结果如下:

    当前用来对比的位置是:0,数据是:1
    现在用1和1进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用1和3进行对比
    [3, 1, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用3和5进行对比
    [5, 1, 3, 9, 70, 135, 12, 17, 24, 34]
    现在用5和9进行对比
    [9, 1, 3, 5, 70, 135, 12, 17, 24, 34]
    现在用9和70进行对比
    [70, 1, 3, 5, 9, 135, 12, 17, 24, 34]
    现在用70和135进行对比
    [135, 1, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和12进行对比
    [135, 1, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和17进行对比
    [135, 1, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和24进行对比
    [135, 1, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和34进行对比
    [135, 1, 3, 5, 9, 70, 12, 17, 24, 34]
    ======当前位置:0,数据:135 已结束==========
    当前用来对比的位置是:1,数据是:1
    现在用1和135进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和135进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和3进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和5进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和9进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和70进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和12进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和17进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和24进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用135和34进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    ======当前位置:1,数据:135 已结束==========
    当前用来对比的位置是:2,数据是:3
    现在用3和1进行对比
    [1, 135, 3, 5, 9, 70, 12, 17, 24, 34]
    现在用3和135进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用135和135进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用135和5进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用135和9进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用135和70进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用135和12进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用135和17进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用135和24进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用135和34进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    ======当前位置:2,数据:135 已结束==========
    当前用来对比的位置是:3,数据是:5
    现在用5和1进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用5和3进行对比
    [1, 3, 135, 5, 9, 70, 12, 17, 24, 34]
    现在用5和135进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用135和135进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用135和9进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用135和70进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用135和12进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用135和17进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用135和24进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用135和34进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    ======当前位置:3,数据:135 已结束==========
    当前用来对比的位置是:4,数据是:9
    现在用9和1进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用9和3进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用9和5进行对比
    [1, 3, 5, 135, 9, 70, 12, 17, 24, 34]
    现在用9和135进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用135和135进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用135和70进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用135和12进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用135和17进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用135和24进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用135和34进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    ======当前位置:4,数据:135 已结束==========
    当前用来对比的位置是:5,数据是:70
    现在用70和1进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用70和3进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用70和5进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用70和9进行对比
    [1, 3, 5, 9, 135, 70, 12, 17, 24, 34]
    现在用70和135进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用135和135进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用135和12进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用135和17进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用135和24进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用135和34进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    ======当前位置:5,数据:135 已结束==========
    当前用来对比的位置是:6,数据是:12
    现在用12和1进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用12和3进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用12和5进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用12和9进行对比
    [1, 3, 5, 9, 70, 135, 12, 17, 24, 34]
    现在用12和70进行对比
    [1, 3, 5, 9, 12, 135, 70, 17, 24, 34]
    现在用70和135进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用135和135进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用135和17进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用135和24进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用135和34进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    ======当前位置:6,数据:135 已结束==========
    当前用来对比的位置是:7,数据是:17
    现在用17和1进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用17和3进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用17和5进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用17和9进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用17和12进行对比
    [1, 3, 5, 9, 12, 70, 135, 17, 24, 34]
    现在用17和70进行对比
    [1, 3, 5, 9, 12, 17, 135, 70, 24, 34]
    现在用70和135进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用135和135进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用135和24进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用135和34进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    ======当前位置:7,数据:135 已结束==========
    当前用来对比的位置是:8,数据是:24
    现在用24和1进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用24和3进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用24和5进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用24和9进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用24和12进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用24和17进行对比
    [1, 3, 5, 9, 12, 17, 70, 135, 24, 34]
    现在用24和70进行对比
    [1, 3, 5, 9, 12, 17, 24, 135, 70, 34]
    现在用70和135进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用135和135进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用135和34进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    ======当前位置:8,数据:135 已结束==========
    当前用来对比的位置是:9,数据是:34
    现在用34和1进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用34和3进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用34和5进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用34和9进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用34和12进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用34和17进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用34和24进行对比
    [1, 3, 5, 9, 12, 17, 24, 70, 135, 34]
    现在用34和70进行对比
    [1, 3, 5, 9, 12, 17, 24, 34, 135, 70]
    现在用70和135进行对比
    [1, 3, 5, 9, 12, 17, 24, 34, 70, 135]
    现在用135和135进行对比
    [1, 3, 5, 9, 12, 17, 24, 34, 70, 135]
    ======当前位置:9,数据:135 已结束==========
    [1, 3, 5, 9, 12, 17, 24, 34, 70, 135]

  • 相关阅读:
    [总结]2020年8月 OI学习/刷题记录
    [总结]2020年7月 OI学习/刷题记录
    [总结]2020年6月 OI学习/刷题记录
    [总结]2020年5月 OI学习/刷题记录
    [Luogu P6474][NOI Online #2 入门组]荆轲刺秦王 题解(BFS)
    [总结]2020年4月 OI学习/刷题记录
    [总结]2020年3月 OI学习/刷题记录
    [Luogu P6059]纯粹容器 senpai
    [Luogu P6044]「ACOI2020」惊吓路径
    ubuntu下查看(改变)本地端口开放情况,开启和关闭防火墙
  • 原文地址:https://www.cnblogs.com/huajie-chj/p/14239601.html
Copyright © 2011-2022 走看看