菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt Python for 循环 Python while 循环
(追記) (追記ここまで)

Python 冒泡排序

Document 对象参考手册 Python3 实例

冒泡排序是一种简单的交换排序算法,其核心工作原理是:

  1. 重复地遍历待排序的列表,一次比较相邻的两个元素;
  2. 如果两个元素的顺序不符合要求(比如升序排序时前一个元素大于后一个),就交换它们的位置;
  3. 每一轮遍历都会将当前未排序部分的最大元素冒泡到末尾(升序场景),如同气泡上浮一样;
  4. 重复上述过程,直到整个列表完全有序(没有交换操作发生或遍历完所有元素)。

关键特性

  • 排序类型:交换排序
  • 时间复杂度:最坏情况/O(n2)(列表完全逆序)、平均情况/O(n2)、最好情况/O(n)(优化版,列表已有序)
  • 空间复杂度:O(1)(原地排序,无需额外辅助空间)
  • 稳定性:稳定排序(相等元素的相对位置不会改变)

实例

defbubbleSort(arr): n = len(arr)# 遍历所有数组元素foriinrange(n): # 最后 i 个元素已经处于正确的位置(无需再比较)forjinrange(0, n-i-1): ifarr[j] > arr[j+1] : arr[j], arr[j+1] = arr[j+1], arr[j]arr = [64, 34, 25, 12, 22, 11, 90]bubbleSort(arr)print("排序后的数组:")foriinrange(len(arr)): print("%d" %arr[i]),

执行以上代码输出结果为:

排序后的数组:
11
12
22
25
34
64
90

Document 对象参考手册 Python3 实例

AI 思考中...

3 篇笔记 写笔记

  1. #0

    小花花

    124***[email protected]

    38

    参考:

    """冒泡排序
    Bubblesort.py"""
    from random import randrange, shuffle
    def Bubblesort():
     array = []
     while len(array) < 12: # 范围内随机取12个数值
     array.append(randrange(-99, 101, 3))
     shuffle(array) # 打乱数组
     print('排序前数组:{}'.format(array))
     for i in range(12):
     for j in range(11 - i):
     if array[j] > array[j + 1]: # 遇到较小值前后交换
     array[j], array[j + 1] = array[j + 1], array[j]
     print('排序后数组:{}'.format(array))
    Bubblesort()

    小花花

    124***[email protected]

    7年前 (2019年07月14日)
  2. #0

    JonnyHuang

    joo***[email protected]

    29

    可选择升序和降序。

    #可选升降序的冒泡排序, order>0升序,order<0降序
    def bubbleSort(arr,order):
     max = len(arr)
     for i in range(0, max):
     j = 1
     while(j<max-i):
     if((arr[j-1]>arr[j]) and (int(order)>0)) or ((arr[j-1]<arr[j]) and (int(order)<0)):
     arr[j-1], arr[j] = arr[j], arr[j - 1]
     j += 1
     i += 1
     return arr
    A = [64, 25, 12, 22, 11] 
    print(bubbleSort(A, -1))
    print(bubbleSort(A, 1))

    JonnyHuang

    joo***[email protected]

    7年前 (2019年07月21日)
  3. #0

    BetyOne

    131***[email protected]

    16

    实现冒泡法排序时要考虑时间复杂度和空间复杂度是靠近O(1) ,还是接近O(n),甚至是O(n2),时间复杂度大的算法会增加计算机运行负荷,效率低下。尽量写出时间复杂度小的算法。

    实例一:

    # 冒泡法 排序
    nums = random.sample(range(1000),9)
    length = len(nums)
    for i in range(length): # 控制循环多少次
     flag = True #假设无需交换 
     for j in range(length-1-i):
     if nums[j] > nums[j+1]:
     nums[j],nums[j+1] = nums[j+1],nums[j] # 大数与小数互换
     flag = False # 如果交换了 就推翻假设
     if flag: # 如果假设成立,则结束交换
     break
    print(nums)

    实例二(最基础的if循环语句实现排序法)

    nums = random.sample(range(1,1000),3)
    order = None
    if nums[0] > nums[1]:
     if nums[1] > nums[2]:
     order = [2,1,0]
     else:
     if nums[0] > nums[2]:
     order = [1,2,0]
     else:
     order = [1,0,2]
    else:
     if nums[0] > nums[2]:
     order = [2,0,1]
     else:
     if nums[1] > nums[2]:
     order = [0,2,1]
     else:
     order = [0,1,2]
    for i in order:
     print(nums[i])

    实例三 (sort排序法)

    nums = random.sample(range(1000),20)
    nums.sort(reverse = True) # sort 排序法

    多多练习各种排序法,不仅限于冒泡排序,分析计算他们的时间复杂度。

    BetyOne

    131***[email protected]

    6年前 (2020年04月08日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /