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

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 实例

插入排序(英语:Insertion Sort)是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。

实例

definsertionSort(arr): foriinrange(1, len(arr)): key = arr[i]j = i-1whilej >=0andkey < arr[j] : arr[j+1] = arr[j]j -= 1arr[j+1] = keyarr = [12, 11, 13, 5, 6]insertionSort(arr)print("排序后的数组:")foriinrange(len(arr)): print("%d" %arr[i])

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

排序后的数组:
5
6
11
12
13

Document 对象参考手册 Python3 实例

AI 思考中...

5 篇笔记 写笔记

  1. #0

    小花花

    124***[email protected]

    5

    主函数迭代次数:数值个数n 0.5n(n + 1)。

    """插入排序
    InsertionSort.py"""
    # 一次往数组添加多个数字
    def AppendNumbers(array):
     num = input('Numbers:(split by spaces)\t').split()
     for i in num:
     array.append(int(i))
     print('排序前数组:{}.'.format(array))
    def InsertionSort(array):
     AppendNumbers(array) # 添加
     list = []
     while True:
     for i in array:
     minimum = min(array)
     if i == minimum:
     list.append(i)
     array.remove(i) # 删去最小值
     if array == []:
     break
     print('排序后数组:{}.'.format(list))
    array = [6, 4, 45, -2, -1, 2, 4, 0, 1, 2, 3, 4, 5, 6, -4, -6, 7, 8, 8, 34, 0]
    InsertionSort(array)

    小花花

    124***[email protected]

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

    陈某某

    904***[email protected]

    89

    参考方法:

    arr = [1,12,2, 11, 13, 5, 6,18,4,9,-5,3,11] 
    def insertionSort(arr):
     #从要排序的列表第二个元素开始比较
     for i in range(1,len(arr)):
     j = i
     #从大到小比较,直到比较到第一个元素
     while j > 0:
     if arr[j] < arr[j-1]:
     arr[j-1],arr[j] = arr[j],arr[j-1]
     j -= 1 
     return arr
    print(insertionSort(arr))

    陈某某

    904***[email protected]

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

    大嘴

    144***[email protected]

    4

    参考:

    def insertionSort():
     import random
     random.seed(888)
     data=[]
     for i in range(15):
     data.append(random.randint(1, 100))
     print(data)
     for i in range(1,len(data)):
     temp=data[i]
     for j in reversed(range(i)):
     if temp<data[j]:
     data[j+1],data[j]=data[j],temp
     print(data)
    insertionSort()

    大嘴

    144***[email protected]

    7年前 (2019年11月04日)
  4. #0

    我是一只菜菜鸟

    348***[email protected]

    10

    另外的实现方法:

    # 插入排序算法实现
    def insertionSort(list):
     n = len(list)
     for i in range(1,n): #默认数列中的第一个元素已排好,故从第二个元素开始插入 
     j = i
     while list[j] < list[j-1] and j>0: #和已经排好序的数从右往左比较,小于则交换位置 
     list[j],list[j-1] = list[j-1],list[j]
     j-=1 
     return list
    list=[2,1,0,5,7,4,0]
    print(insertionSort(list))

    我是一只菜菜鸟

    348***[email protected]

    6年前 (2020年09月01日)
  5. #0

    sky

    127***[email protected]

    5

    关键在于两组数据,一个是排好的 b,一个是待处理的 a。待处理的(第二个 for)如果小于处理好的,就插入,然后删除原来的。没有替换的关系

    l=[2,5,1,4,6,9,3,0,8,7]
    for a in range(1,len(l)):
     for b in range(0,a):
     if l[a]<l[b]:
     l.insert(b,l[a])
     del l[a+1]
     print(l)

    输出结果为:

    [1, 2, 5, 4, 6, 9, 3, 0, 8, 7]
    [1, 2, 4, 5, 6, 9, 3, 0, 8, 7]
    [1, 2, 3, 4, 5, 6, 9, 0, 8, 7]
    [0, 1, 2, 3, 4, 5, 6, 9, 8, 7]
    [0, 1, 2, 3, 4, 5, 6, 8, 9, 7]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    sky

    127***[email protected]

    4年前 (2022年05月31日)

点我分享笔记

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

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