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 ...
随机推荐
- 系列文章----.Net程序员学用Oracle系列
.Net程序员学用Oracle系列(18):PLSQL Developer 攻略 .Net程序员学用Oracle系列(17):数据库管理工具(SQL Plus) .Net程序员学用Oracle系列(1 ...
- 基于BP神经网络的字符识别研究
基于BP神经网络的字符识别研究 原文作者:Andrew Kirillov. http://www.codeproject.com/KB/cs/neural_network_ocr.aspx 摘要:本文 ...
- 媒体查询ipad,pc端
媒体查询 /* 判断ipad */ @media only screen and (min-device-width : 768px) and (max-device-width : 1024px){ ...
- 蓝桥杯 算法训练 ALGO-151 6-2递归求二进制表示位数
算法训练 6-2递归求二进制表示位数 时间限制:10.0s 内存限制:256.0MB 问题描述 给定一个十进制整数,返回其对应的二进制数的位数.例如,输入十进制数9,其对应的二进制数是1001 ...
- 机器学习:PCA(实例:MNIST数据集)
一.数据 获取数据 import numpy as np from sklearn.datasets import fetch_mldata mnist = fetch_mldata("MN ...
- C# winfrom FastReport 变量设计加载
1.源码 DataTable dt5 = new DataTable(); dt5 = SqlHelper.SqlGetDataTable(StrSql, "tbEmpCont") ...
- Task Crontab
Crontab 1.查看任务 crontab -l 2.编辑任务 1)对应用户登录后编辑其下的作业 crontab -e 2)删除指定用户任务 crontab -u user -r 3)删除用户下指定 ...
- 某个应用使cpu使用率100%
--CPU使用率 Linux是一个多任务的操作系统,将每个cpu的时间划分为很短的时间片,再通过调度器轮流分配给各个任务使用,因此造成多任务同时运行的错觉 为了维护cpu时间,linux通过事先定义的 ...
- jenkins学习 02 jenkins安装与配置
1: Jenkins安装 在最简单的情况下,Jenkins 只需要两个步骤: (1)下载最新的版本(一个 WAR 文件).Jenkins官方网址: http://Jenkins-ci.org/ (2 ...
- c# linq查询语句详细使用介绍
本文介绍Linq的使用方法 linq介绍 LINQ只不过是实现IEnumerable和IQueryable接口的类的扩展方法的集合. LINQ可以查询IEnumerable集合或者IQueryable ...