[Algorithms] Insertion sort algorithm using TypeScript
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的更多相关文章
- [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] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- Algorithms - Insertion sort
印象 图1 插入排序过程 思想 插入排序(Insertion Sort)的主要思想是不断地将待排序的元素插入到有序序列中,是有序序列不断地扩大,直至所有元素都被插入到有序序列中. 分析 时间复杂度: ...
- Algorithms - Insertion Sort - 插入排序
Insertion Sort - 插入排序 插入排序算法的 '时间复杂度' 是输入规模的二次函数, 深度抽象后表示为, n 的二次方. import time, random F = 0 alist ...
- The insertion sort algorithm expressed in pseudocode - 插入排序
Computer Science An Overview _J. Glenn Brookshear _11th Edition procedure Sort (List) N ← 2; while ( ...
- [Algorithms] Quicksort algorithm using TypeScript
Quicksort (also called partition sort and pivot sort) is arguably the most used sorting algorithm. I ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- 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 ...
随机推荐
- 程序员段子:世界上最大的同性交友平台github
程序员(又名程序猿)因为总是冲锋在网络的最前端,还有程序猿的各种特殊性,大家在茶余饭后都有很多关于程序员的段子流传.大多都是程序员自黑的,先说在前面,程序猿还是很好的!下面看看你有没有中枪的那一条呢? ...
- LeetCode137只出现一次的数字——位运算
题目 题目描述:给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现三次.找出那个只出现一次的元素. 说明:你的算法应该具有线性时间的复杂度.你可以不使用额外的空间来实现吗? 思路 题 ...
- 【原】jq简易教程
https://www.jianshu.com/p/3522fe70de19 https://www.jianshu.com/p/6de3cfdbdb0e 1. jq简介 jq可以对json数据进行分 ...
- pom.xml配置引用项目时不生效
1 在项目pom.xml配置中引用项目A,但是编译时,取提数引起是B: 2 原因是:[Java Build Path - Projects] 引用的还是老的项目B,删除该引用即可解决.
- 第3节 hive高级用法:16、17、18
第3节 hive高级用法:16.hive当中常用的几种数据存储格式对比:17.存储方式与压缩格式相结合:18.总结 hive当中的数据存储格式: 行式存储:textFile sequenceFile ...
- JavaScript:JSON 和 JS 对象
区别 JSON(JavaScript Object Notation)仅仅是一种数据格式(或者叫数据形式).数据格式其实就是一种规范,按照这种规范来存诸和交换数据.就好像 XML 格式一样. 区别 J ...
- find_in_set()和in()比较
转载于:https://www.cnblogs.com/zqifa/p/mysql-4.html 作者:zqifa 因为自己太懒了,就从大佬那转载来,作为一次笔记! mysql 中find_in_se ...
- 制作framework&静态库
http://blog.csdn.net/justinjing0612/article/details/7880712 (制作framework) http://blog.sina.com.c ...
- LeetCode1-5
Leetcode1: Given an array of integers, return indices of the two numbers such that they add up to a ...
- 有向图连通分量SCC
在无向图中,如果从顶点vi到顶点vj有路径,则称vi和vj连通.如果图中任意两个顶点之间都连通,则称该图为连通图,否则,称该图为非连通图,则其中的极大连通子图称为连通分量,这里所谓的极大是指子图中包含 ...