Algorithms - Insertion sort
印象
图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;
}
}
参考
Algorithms - Insertion sort的更多相关文章
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- [Algorithms] Insertion sort algorithm using TypeScript
Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...
- Algorithms - Insertion Sort - 插入排序
Insertion Sort - 插入排序 插入排序算法的 '时间复杂度' 是输入规模的二次函数, 深度抽象后表示为, n 的二次方. import time, random F = 0 alist ...
- 【LeetCode OJ】Insertion Sort List
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...
- 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 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- 经典排序算法 – 插入排序Insertion sort
经典排序算法 – 插入排序Insertion sort 插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...
- leetcode Insertion Sort List
题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...
随机推荐
- 常用Kerberos指令
1. 以超管身份进入kadminkadmin.local addprinc -randkey root/master1@JENKIN.COM //生成随机key的principal addprin ...
- BZOJ3745:[COCI2015]Norma
浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...
- CentOS7下Supervisor安装与配置
Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统 ...
- 导入镜像后,容器内httpd起不来
导入镜像后发现bash进程为1 与之前apache启动的进程冲突了 解决办法:删除apache进程号,通过apachectl重启apache进程
- POJ3258(最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11155 Accepted: 4785 ...
- JavaScript去除字符串两边空格trim
去除字符串左右两端的空格,在大部分编程语言中,比如PHP.vbscript里面可以轻松地使用 trim.ltrim 或 rtrim实现.但在js中却没有这3个内置方法,需要手工编写.下面的实现方法是用 ...
- [cinder] volume type 使用简记
cinder type-create sharecinder type-key share set volume_backend_name=GLUSTERFScinder type-create lo ...
- Jenkins关闭、重启,Jenkins服务的启动、停止方法。
一.Jenkins关闭.重启 1.关闭Jenkins 只需要在访问jenkins服务器的网址url地址后加上exit,关闭Jenkins服务. 例如:http://localhost:8081/ex ...
- Oracle 文件
参数文件 跟踪文件 告警文件 数据文件 临时文件 控制文件 重做日志文件 密码文件 闪回日志 dum文件 数据泵文件 1参数文件 Parameter file:告诉oracle实例在那里可以找到控制文 ...
- c# 使用GetPrivateProfileString 读ini数据 失败
项目中用到 GetPrivateProfileString但是使用中, 发现 无法读出 ini 配置中的值, 比如Enable_log =3 我读到的是 API设置的默认值. 网上说可能时字符集编码的 ...