[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 ...
随机推荐
- iOS开发 数据缓存-数据库
iOS中数据存储方式 Plist(NSArray\NSDictionary) Preference(偏好设置\NSUserDefaults) NSCoding (NSKeyedArchiver\NSk ...
- Python3中assert断言
一般的用法是: assert condition 用来让程序测试这个condition,如果condition为false,那么raise一个AssertionError.逻辑上等于: if not ...
- find_in_set()和in()比较
转载于:https://www.cnblogs.com/zqifa/p/mysql-4.html 作者:zqifa 因为自己太懒了,就从大佬那转载来,作为一次笔记! mysql 中find_in_se ...
- java IO流 内容整理
在java中,对数据的输入和输出操作以流的方式进行.(注:对文件的操作用io.File类,但不能对文件中的内容进行操作) 一.IO流的分类: 按数据流的方向不同,可以分为输入流和输出流: 按处理数据的 ...
- 黑马毕向东Java基础知识总结
Java基础知识总结(超级经典) 转自:百度文库 黑马毕向东JAVA基础总结笔记 侵删! 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部 ...
- JavaScript中整型数据使用
JavaScript中整型数据使用 制作人:全心全意 JavaScript的数字格式允许精确地表示-900719925474092(-253)和900719925474092(253)之间的所有整数, ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
- UVA 221 城市化地图(离散化思想)
题意: 给出若干个栋楼俯视图的坐标和面积,求从俯视图的南面(可以视为正视图)看过去到底能看到多少栋楼. 输入第一个n说明有n栋楼,然后输入5个实数(注意是实数),分别是楼的左下角坐标(x,y), 然后 ...
- python面向对象编程设计与开发
一.什么是面向对象的程序设计 1.何为数据结构? 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合,如列表.字典. 2.何为编程? 编程是指程序员用特定的语法+数据结构+算法,组成的代码,告 ...
- 洛谷 P2335 SDOI 2005 毒瘤 位图(也补上注释了)
#include<iostream> #include<cstdio> #include<queue> #include<cstring> using ...