印象

图1 插入排序过程

思想

插入排序(Insertion Sort)的主要思想是不断地将待排序的元素插入到有序序列中,是有序序列不断地扩大,直至所有元素都被插入到有序序列中。

分析

  • 时间复杂度:

    • 最优时间: O(\(n-1\))
    • 最坏时间: O(\(\frac{1}{2}n(n-1)\))
    • 平均时间: O(\(n^2\))

插入排序不适合对于数据量比较大的排序应用。但是,如果需要排序的数据量很小,例如,量级小于千;或者若已知输入元素大致上按照顺序排列,那么插入排序还是一个不错的选择。 插入排序在工业级库中也有着广泛的应用,在STL的sort算法和stdlib的qsort算法中,都将插入排序作为快速排序的补充,用于少量元素的排序(通常为8个或以下)。

代码示例

C++

void insertion_sort(int arr[], int len)
{
for (int i = 1; i < len; i++)
{
int key = arr[i];
int j = i - 1;
while ((j >= 0) && (key < arr[j]))
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

参考

Wikipedia - Insertion sort

Algorithms - Insertion sort的更多相关文章

  1. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  2. [Algorithms] Insertion sort algorithm using TypeScript

    Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...

  3. Algorithms - Insertion Sort - 插入排序

    Insertion Sort - 插入排序 插入排序算法的 '时间复杂度' 是输入规模的二次函数, 深度抽象后表示为, n 的二次方. import time, random F = 0 alist ...

  4. 【LeetCode OJ】Insertion Sort List

    Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...

  5. Codeforces Round #212 (Div. 2) C. Insertion Sort

    C. Insertion Sort Petya is a beginner programmer. He has already mastered the basics of the C++ lang ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  7. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  8. 经典排序算法 – 插入排序Insertion sort

    经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...

  9. leetcode Insertion Sort List

    题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...

随机推荐

  1. centos7 node express项目 将http接口升级为https接口的解决方法

    1.将对应的ssl证书放到项目文件中(我装的镜像是oneinstack的,创建项目时可以直接选择生成ssl证书的项目,即 xxx.key 和 xxx.crt 文件): 2.修改bin/www文件,修改 ...

  2. node.js 笔记(一)

    参考:https://github.com/alsotang/node-lessons 感谢!!! 本文属于小白入门级笔记,请大牛自动屏蔽!!! 1.     开发环境 os: 10.12.6 nod ...

  3. Python利用itchat库向好友或者公众号发消息

    首先获得好友或者公众号的UserName 1.获取好友UserName #coding=utf8 import itchat itchat.auto_login(hotReload=True) #想给 ...

  4. SPI编程1:用户空间的读写操作

    spi_device 虽然用户空间不需要直接用到spi_device结构体,但是这个结构体和用户空间的程序有密切的关系,理解它的成员有助于理解SPI设备节点的IOCTL命令,所以首先来介绍它.在内核中 ...

  5. ES之二:Elasticsearch原理

    Elasticsearch是最近两年异军突起的一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建.最近研究了一下,感觉 Elasticsearch 的架构以及其开源的生态 ...

  6. git学习5 ecipse集成git(转载)

    原文地址:http://blog.csdn.net/hhhccckkk/article/details/10458159 有的eclipse已经自带了GIt了,就不用安装了 1: 进行安装Git,和安 ...

  7. [置顶] 自己写一个简单通用的Makefile

    转自:http://blog.csdn.net/u011913612/article/details/52102241 一.makefile的作用 Makefile是用于自动编译和链接的,一个工程有很 ...

  8. BIOS简单设置 解析“集成显卡”内存占用问题

    很多使用集成显卡的用户会发现,在系统信息窗口中,内存容量和实际不一样.比如系统内存显示4GB,可用3.48G之类.这不可用的一部分内存到哪去了? 其实减少的这部分内存是被集成显卡占用当做显存使用了.而 ...

  9. Ubuntu bash不记录history方法

    很多都是用: unset HISTORY HISTFILE HISTSAVE HISTZONE HISTORY HISTLOG export HISTFILE=/dev/null export HIS ...

  10. Windows Bypass UAC

    Windows 10 sdclt UAC bypass @echo off reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\A ...