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. python面试题解析(python基础篇80题)

    1.   答:出于编程的喜爱,以及行业本身的前瞻性,创造性,优越性,越是综合的科目越能检验一个人的能力,喜欢这种有挑战的事情. 2.   答:跟随老师学习,以及自己查询资料,结合实战,进行输入输出以及 ...

  2. 28、editText只输入英文字母和'-',用于授权码输入

    InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, i ...

  3. python算法-汉诺塔问题

    汉诺塔问题   初始状态: 思考:当盘子的个数是3的时候,大家写出移动顺序 移动的步骤: 3个盘子,从a到c 1.前面两个盘子,从a到b 1)把前面一个盘子,从a到c a->c 2)把第二个盘子 ...

  4. [办公软件篇][2]source-insight安装

    http://jingyan.baidu.com/article/a3f121e4fe173ffc9052bbb0.html 注意看:同步代码

  5. IO Streams:字节流

    简介 程序使用字节流来执行8位字节的输入和输出.所有字节流类都继承于InputStream和OutputStream. 有很多字节流类:为了说明字节流如何工作,我们将重点关注文件I / O字节流Fil ...

  6. failed to lazily initialize a collection of role 异常

    最近在通过配置实体类的方式,正向自动扫描注解方式配置的hibernate类文件来生成数据库的方法搭建环境,遇到了许多问题. 通过数据库配置hibernate的时候,大家都知道是在实体类对应生成的.hb ...

  7. JDBC 学习笔记(五)—— Statement

    JDBC 使用 Statement 作为 SQL 语句的执行器. Statement 通过 Connection.createStatement() 方法创建,一共支持以下6种方式执行 SQL 语句: ...

  8. hdu6097[二分+解析几何] 2017多校6

    /*hdu6097[二分+解析几何] 2017多校6*/ #include <bits/stdc++.h> using namespace std; ; struct node{ doub ...

  9. [CODEVS1914] 运输问题(最小费用最大流)

    传送门 水题. 建图都不想说了 ——代码 #include <queue> #include <cstdio> #include <cstring> #includ ...

  10. [JSOI2007]字符加密Cipher SA

    [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7859  Solved: 3410[Submit][Stat ...