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

推荐订阅源

P
Palo Alto Networks Blog
P
Proofpoint News Feed
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
罗磊的独立博客
J
Java Code Geeks
月光博客
月光博客
F
Full Disclosure
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
U
Unit 42
WordPress大学
WordPress大学
A
About on SuperTechFans
C
Cyber Attacks, Cyber Crime and Cyber Security
SecWiki News
SecWiki News
Security Latest
Security Latest
C
Check Point Blog
C
CERT Recently Published Vulnerability Notes
小众软件
小众软件
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
V
Visual Studio Blog
博客园_首页
NISL@THU
NISL@THU
I
Intezer
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
The Register - Security
The Register - Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Latest news
Latest news
Project Zero
Project Zero
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
博客园 - 【当耐特】
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
V
Vulnerabilities – Threatpost
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网

博客园 - looping

小米公司招聘前端--待遇不错 JSLint for EditPlus 检验js语法 ECMA-262-3 深入解析.第四章. 作用域链 Vim 简洁手册 在 JavaScript 中监听 IME 键盘输入事件 英文已如此搞笑,翻译却更加残暴 古文中惊艳的句子 Nicholas C. Zakas vs John Resig 一场关于YUI3/jQuery的精彩辩论 浅谈 Mousewheel 事件 css 圆角效果,2例 CSS HACK org.hibernate.TransientObjectException: object references an unsaved t... jquery 1.4 append 用法 获取文本中img路径(不是dom中的) [JavaScript]ECMA-262 深入解析 IIS 7.5 下PHP(FastCGI模式)配置手记 Remove duplicates from Array移除数组重复元素 js 翻转颜色 difference between echo and print in php
插入排序 insertion_sort js实现
looping · 2011-09-26 · via 博客园 - looping

一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:

  1. 从第一个元素开始,该元素可以认为已经被排序
  2. 取出下一个元素,在已经排序的元素序列中从后向前扫描
  3. 如果该元素(已排序)大于新元素,将该元素移到下一位置
  4. 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
  5. 将新元素插入到该位置中
  6. 重复步骤

伪代码如下:

INSERTION-SORT(A)
     
for jto length[A]
         
do keyA[J]
         i
j-1
         
while i>0 and A[i]<key
             
do A[i+1]←A[i]
             i
i-1
         A
[i+1]←key

 js实现如下:

 1 var arr = [5,2,4,6,1,3],key;
 2 for(var j = 1; j < arr.length ; j++){ 
 3    //debugger;
 4     //排好序的
 5     var i = j - 1;
 6     key = arr[j];
 7     while(i >= 0 && arr[i] > key){  
 8         arr[i + 1] = arr[i];       
 9         i --;        
10     }
11     arr[i + 1] = key;
12 }