python 插入排序

2023-05-16

问题:数组排序

插入排序,向已经有序一组序列中,插入一个新的元素
默认第一个列表元素为已经排序好的元素,从第二个元素进行比较,
已经排序好的元素,重大到小,依次进行比较,最后得到结果

import traceback


def insert_sort(wait_sort_data: list, reverse=True):
    """
    插入排序的实现
    :param wait_sort_data: 待排序的列表数据
    :param reverse: 是否反序操作
    :return:
    """
    try:
        if not wait_sort_data:
            raise Exception('当前要排序的列表为空,请检查')
        if reverse:
            for index in range(1, len(wait_sort_data)):
                key = wait_sort_data[index]
                upper_index = index - 1
                while upper_index >= 0 and wait_sort_data[upper_index] > key:
                    wait_sort_data[upper_index + 1] = wait_sort_data[upper_index]
                    upper_index -= 1
                wait_sort_data[upper_index + 1] = key
        else:
            for index in range(len(wait_sort_data) - 2, -1, -1):
                key = wait_sort_data[index]
                after_index = index + 1
                while after_index < len(wait_sort_data) and wait_sort_data[after_index] > key:
                    wait_sort_data[after_index - 1] = wait_sort_data[after_index]
                    after_index += 1
                wait_sort_data[after_index - 1] = key
        return wait_sort_data
    except Exception as e:
        if str(e).count('请检查'):
            raise e
        raise Exception(str(e) + '插入排序实现发生异常,原因:{}'.format(traceback.format_exc()))


if __name__ == "__main__":
    new_data = insert_sort([1, 500, 487, 0, 792, 12, 654, 438, 78, -123, -345], reverse=False)
    print(new_data)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 插入排序 的相关文章

随机推荐

  • Centos系统安装RabbitMQ消息中间件

    记录一下在centos7 x下面安装RabbitMQ消息中间件 RabbitMQ是一个开源而且遵循 AMQP协议实现的基于 Erlang语言编写 xff0c 因此安装RabbitMQ之前是需要部署安装Erlang环境的 先安装Erlang
  • SpringBoot+RXTXcomm实现Java串口通信 读取串口数据以及发送数据

    记录一下使用SpringBoot 43 RXTXcomm实现Java串口通信 xff0c 使用Java语言开发串口 xff0c 对串口进行读写操作 RXTXcomm jar这个包支持的系统较多 xff0c 但是更新太慢 xff0c 在win
  • Springboot+Netty搭建TCP服务端

    Netty是业界最流行的nio框架之一 xff0c 它具有功能强大 性能优异 可定制性和可扩展性的优点 Netty的优点 xff1a 1 API使用简单 xff0c 开发入门门槛低 2 功能十分强大 xff0c 预置多种编码解码功能 xff
  • Springboot+Netty搭建TCP客户端-多客户端

    之前搭建了一个Springboot 43 Netty服务端的应用 xff0c 既然有服务端 xff0c 自然也有客户端的应用 xff0c 现在搭建一个Springboot 43 Netty客户端的应用Demo程序 xff0c 多客户端方式
  • 机器学习中的凸和非凸优化问题

    题目 xff08 145 xff09 xff1a 机器学习中的优化问题 xff0c 哪些是凸优化问题 xff0c 哪些是非凸优化问题 xff1f 请各举一个例子 凸优化定义 凸优化问题 非凸优化问题 凸优化定义 xff1a 公式 geome
  • VMware workstation中rhel安装VMware tools失败

    切换登录用户为root即可 转载于 https www cnblogs com dazzleC p 10555809 html
  • Uniform convergence may be unable to explain generalization in deep learning

    本文价值 xff1a understand the limitations of u c based bounds cast doubt on the power of u c bounds to fully explain general
  • 调参之learning rate

    The learning rate is perhaps the most important hyperparameter If you have time to tune only one hyperparameter tune the
  • 调超参(lr,regularization parameter)经验整理

    Learning rate 最优值从1e 4到1e 1的数量级都碰到过 xff0c 原则大概是越简单的模型的learning rate可以越大一些 https blog csdn net weixin 44070747 article de
  • Dropout network, DropConnect network

    Notations input v v v output r r r weight parameter
  • Curriculum adversarial training

    Weakness of adversarial training overfit to the attack in use and hence does not generalize to test data Curriculum adve
  • Python处理中文语言——读取中文

    本文解决问题 xff1a 1 导入中文txt文本 xff0c 并转换为unicode 2 导入包含中文的py file 解决问题一 xff1a 导入中文txt文本 xff0c 并转换为unicode 基础概念 xff1a 1 unicode
  • C# WPF开源控件库HandyControl用法举例

    目录 概述 MessageBox用法举例 Button用法举例 Lable用法举例 Slider用法举例 TextBox用法举例 组合框ComboBox用法举例 源码下载 概述 HandyControl是一款免费开源的WPF控件库 xff0
  • python 等差数列生成器

    典型的迭代器模式作用很简单 遍历数据结构 不过 xff0c 即便不是从集合中获取元素 xff0c 而 是获取序列中即时生成的下一个值时 xff0c 也用得到这种基于方法的标准接口 例如 xff0c 内置的 range 函数用于生成有穷整数等
  • python 终止协程和异常处理

    协程中未处理的异常会向上冒泡 xff0c 传给 next 函数或 send 方法的调用方 xff08 即触发协程的对 象 xff09 下面示例举例说明如何使用之前博客示例中由装饰器定义的 averager 协程 未处理的异常会导致协程终止
  • centos7 下安装 nodejs

    源码包安装 下载安装包到 xff1a usr local 目录下 1 命令下载 wget https span class token punctuation span span class token operator span node
  • Ubuntu配置apt软件源

    清华大学开源镜像网站 xff08 帮助页面 xff09 https mirrors tuna tsinghua edu cn help AOSP 阿里云开源镜像网站 https opsx alibaba com mirror 网易开源镜像网
  • python3 fnmatch和fnmatchcase

    你想使用 Unix Shell 中常用的通配符 比如 py Dat 0 9 csv 等 去匹配文本字符串 xff0c fnmatch 模块提供了两个函数 fnmatch 和 fnmatchcase xff0c 可以用来实现这样的匹配 用法如
  • python unicodedata 处理Unicode 字符串

    你正在处理 Unicode 字符串 xff0c 需要确保所有字符串在底层有相同的表示 span class token comment coding utf 8 span span class token comment 你正在处理 Uni
  • python 插入排序

    问题 xff1a 数组排序 插入排序 xff0c 向已经有序一组序列中 xff0c 插入一个新的元素 默认第一个列表元素为已经排序好的元素 xff0c 从第二个元素进行比较 xff0c 已经排序好的元素 xff0c 重大到小 xff0c 依