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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - looping

小米公司招聘前端--待遇不错 JSLint for EditPlus 检验js语法 ECMA-262-3 深入解析.第四章. 作用域链 - looping Vim 简洁手册 在 JavaScript 中监听 IME 键盘输入事件 英文已如此搞笑,翻译却更加残暴 - looping 古文中惊艳的句子 - looping 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 }