Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards in our hands.

In this lesson, using TypeScript / Javascript, we’ll cover how to implement this algorithm, why this algorithm gets its name, and the complexity of our implementation of the insertion algorithm.

/**
* ary: [4,3,2,1]
*
* i = 1:
* current 3
* j = 0 --> array[j + 1] = array[j] --> [4, 4]
* j = -1 --> array[j + 1] = current -->[3, 4]
*
* i = 2:
* current: 2
* j = 1 --> array[j + 1] = array[j] --> [3,4,4]
* j = 0 --> array[j + 1] = array[j] --> [3,3,4]
* j = -1 --> array[j + 1] = current --> [2,3,4]
*/
function insertionSort(ary) {
let array = ary.slice(); for (let i = ; i < array.length; i++) {
let current = array[i];
let j = i - ;
while (j >= && array[j] > current) {
// move the j to j+1
array[j + ] = array[j];
j--;
}
// j = -1
array[j + ] = current;
}
return array;
}

[Algorithms] Insertion sort algorithm using TypeScript的更多相关文章

  1. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  2. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

  3. Algorithms - Insertion sort

    印象 图1 插入排序过程 思想 插入排序(Insertion Sort)的主要思想是不断地将待排序的元素插入到有序序列中,是有序序列不断地扩大,直至所有元素都被插入到有序序列中. 分析 时间复杂度: ...

  4. Algorithms - Insertion Sort - 插入排序

    Insertion Sort - 插入排序 插入排序算法的 '时间复杂度' 是输入规模的二次函数, 深度抽象后表示为, n 的二次方. import time, random F = 0 alist ...

  5. The insertion sort algorithm expressed in pseudocode - 插入排序

    Computer Science An Overview _J. Glenn Brookshear _11th Edition procedure Sort (List) N ← 2; while ( ...

  6. [Algorithms] Quicksort algorithm using TypeScript

    Quicksort (also called partition sort and pivot sort) is arguably the most used sorting algorithm. I ...

  7. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  8. 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 ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

随机推荐

  1. Android(java)学习笔记195:ContentProvider使用之添加数据到联系人(掌握)

    1.添加联系人逻辑思路 (1)首先在raw_contacts创建一个新的id (2)在data表里面添加这个id对应的数据 2.下面通过一个案例,说明一下如何添加一条数据到联系人: (1)首先我们关注 ...

  2. cookie存储位置

    平时各位在做项目时多半时候都会用到客户端的cookie,可大家知道cookie是存储在哪里吗? 首先cookie失效分为2种: 1:设置过期时间失效(只要设置了过期时间cookie就会存储在硬盘里面) ...

  3. OAuth - 第三方登录的原理

    第三方登录的原理 所谓第三方登录,实质就是 OAuth 授权.用户想要登录 A 网站,A 网站让用户提供第三方网站的数据,证明自己的身份.获取第三方网站的身份数据,就需要 OAuth 授权. 举例来说 ...

  4. 导出网页表格数据为Excel文件的前端解决方案

    在工作中,我们有时会遇到这样的需求,比如:要把页面的表格数据导出为Excel文件.在此记录下自己用的解决方法.代码如下: function tableToExcel(data){ //要导出的数据,t ...

  5. 初识 Bootstrap

    Bootstrap 概述 Bootstrap 是一个前端框架,使用它可以快速开发响应式页面,还能专门针对 PC 端或移动端快速开发,大大提高了开发效率. Bootstrap 是最受欢迎的 HTML.C ...

  6. ORM之创建数据库

    ORM之创建数据库 样板:创建表名为UserInfo的表,表的主键可自行写,Django的ORM也可自行创建. from django.db import models class UserInfo( ...

  7. ICE CAVE(BFS搜索(模拟))

    Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...

  8. C语言判断一个数能否被3和5整除

    #include <stdio.h> /* 判断一个数能不能同时被3和5整除 --------soulsjie 20170525----- */ void main(){ int inpu ...

  9. CodeForcesGym 100212E Long Dominoes

    Long Dominoes Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. ...

  10. hihoCoder#1109 最小生成树三·堆优化的Prim算法

    原题地址 坑了我好久...提交总是WA,找了个AC代码,然后做同步随机数据diff测试,结果发现数据量小的时候,测试几十万组随机数据都没问题,但是数据量大了以后就会不同,思前想后就是不知道算法写得有什 ...