惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - smallnest

希尔排序 Comb排序 Gnome sort 鸡尾酒排序 奇偶排序 快速排序 冒泡排序 algorithm in c# 开发人员最喜爱的十大免费的Visual Studio插件(下) 开发人员最喜爱的十大免费的Visual Studio插件(上) 一种获取重载泛型方法的方式 [游戏]五子连珠 发布一个记账软件---流水记账 轻松编写您自己的拖拉机算法,进行算法大战 拖拉机大战更新了 拖拉机大战1.1.0.320发布,更多新功能 拖拉机大战新春贺岁版发布 翻译助手0.1 visual studio 2005 常用类型的图标
插入排序
smallnest · 2009-12-19 · via 博客园 - smallnest

插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法。
它的工作原理是通过构建有序序列,对于未排序数据,
 在已排序序列中从后向前扫描,找到相应位置并插入。
插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),
因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,
为最新元素提供插入空间。

代码

1 using System;
2  using System.Collections.Generic;
3
4 namespace Com.Colobu.Algorithm.Insertion
5 {
6 /// <summary>
7 /// 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法。
8 /// 它的工作原理是通过构建有序序列,对于未排序数据,
9 /// 在已排序序列中从后向前扫描,找到相应位置并插入。
10 /// 插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),
11 /// 因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,
12 /// 为最新元素提供插入空间。
13 ///
14 /// 平均时间复杂度:O(n^2)
15 /// Stability:Yes
16 /// </summary>
17 public class InsertionSortAlgorithm
18 {
19 public static void InsertionSort<T>(IList<T> szArray) where T : IComparable
20 {
21 int count = szArray.Count;
22 int j;
23 T temp;
24 for (int i = 1; i < count; i++)
25 {
26 temp = szArray[i];//store the original sorted array in temp
27 for (j = i; j > 0 && (temp.CompareTo(szArray[j - 1]) < 0); j--)//compare the new array with temp
28 {
29 szArray[j] = szArray[j - 1];//all larger elements are moved one pot to the right
30 }
31 szArray[j] = temp;
32 }
33
34 }
35 }
36 }
37