nsertion sort is another sorting algorithm that closely resembles how we might sort items in the physical world. We start at the second item in our collection and make the assumption that this item is a sorted list of length 1. We then compare all the items before it and determine if it needs to be "inserted" to the left or right of our item. We then move onto the second item, again comparing it to every item before it in the list, inserting those items correctly.

Because this algorithm requires two loops, one inside the other, the worst case scenario of our algorithm still requires a time complexity of O(n^2). This is also an inefficient sorting algorithm, but if our list is mostly sorted already, it will perform a slight bit better than bubble sort.

function insertionSort (array) {
let i = 0
let j = 0 for (i = 1; i < array.length; i++) {
for (j = 0; j < i; j++) {
if (array[i] < array[j]) {
const [item] = array.splice(i, 1); // get the item on ith position
array.splice(j, 0, item);// insert the item on jth position
}
}
} return array;
} let numbers = [10, 5, 6, 3, 2, 8, 9, 4, 7, 1] console.log(insertionSort(numbers))

  

[Algorithms] Sort an Array with a Nested for Loop using Insertion Sort in JavaScript的更多相关文章

  1. 洛谷 SP9722 CODESPTB - Insertion Sort

    洛谷 SP9722 CODESPTB - Insertion Sort 洛谷传送门 题目描述 Insertion Sort is a classical sorting technique. One ...

  2. CF451B Sort the Array 水题

    Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...

  3. [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序

    11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...

  4. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

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

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

  6. Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

    题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...

  7. [Algorithms] Insertion sort algorithm using TypeScript

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

  8. Codeforces Round #258 (Div. 2)——B. Sort the Array

    B. Sort the Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. [LeetCode] 912. Sort an Array 数组排序

    Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Outp ...

随机推荐

  1. (手写)mybatis 核心配置文件和接口不在同一包下的解决方案

    smart-sh-mybatis项目 app.xml文件中此处配置为: <!-- 从整合包里找,org.mybatis:mybatis-spring:1.2.4 --> <!-- s ...

  2. Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)

    题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次.返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1) 举例 ...

  3. sql中group by 和having 用法解析

    --sql中的group by 用法解析:-- Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.--它的作用是通过一定的规则将一个数据集划分成若干个小的 ...

  4. Difference between git remote add and git clone

    http://stackoverflow.com/questions/4855561/difference-between-git-remote-add-and-git-clone git remot ...

  5. 【bzoj1690】[Usaco2007 Dec]奶牛的旅行 分数规划+Spfa

    题目描述 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的城市地图,上面标 ...

  6. VCO的配置方法

    弄了个VCO的环境. 感觉有点儿麻烦,配乱七八糟的服务,弄完了SE也不试试,白弄了.最近又有人说这东西要试试. 我先简单记录下吧: 1. 在vCenter Server 下开启SSO,设置密码. 2. ...

  7. Java面试题之final、finally和finalize的区别

    final: final是一个修饰符,可以修饰变量.方法和类,如果final修饰变量,意味着变量的值在初始化后不能被改变: 防止编译器把final域重排序到构造函数外:(面试的时候估计答出这个估计会加 ...

  8. [转] Makefile 基础 (8) —— Makefile 隐含规则

    该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...

  9. python logger日志工具类

    pytest命令行执行默认不会打印log信息,需要加‘-s’参数或者 ‘–capture=no’,即pytest -s #! /usr/bin/env python # coding=gbk impo ...

  10. 文本生成器(bzoj 1030)

    Description JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以随机生成一些文章―――总是 ...