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 综合应用小项目一

    数据库报错重连机制 利用异常捕获来获取mysql断开的报错,然后再重连 import MySQLdb as mysql class DB: def __init__(self,host,user,pa ...

  2. Java-创建一个线程

    第一种继承Thread类 package com.tj; public class BasicThread1 extends Thread { public void run() { System.o ...

  3. web安全测试---AppScan扫描工具(转)

    安全测试应该是测试中非常重要的一部分,但他常常最容易被忽视掉. 尽管国内经常出现各种安全事件,但没有真正的引起人们的注意.不管是开发还是测试都不太关注产品的安全.当然,这也不能怪我们苦B的“民工兄弟” ...

  4. python - 接口自动化测试实战 - case1 - 优化版

    题目: 基于以下两个接口和数据完成接口自动化测试,并生成测试报告: '''登录 login='http://47.107.168.87:8080/futureloan/mvc/api/member/l ...

  5. Andorid 生成NDK动态链接库 .so库

    .so库第一次见到是在搜索Android保存静态秘钥等特殊id字段做法时看到的-通过NDK的方式将静态秘钥保存在so文件中, 关于原生开发工具包(NDK)详细见官网指南要更详细,这里我记录我度娘各种结 ...

  6. PHP中define()和const定义常量的区别

    在PHP中可以通过define()和const两种方式定义常量可是在开发中我们应该什么时候用define()定义常量,什么时候用const定义常量? 这两种方式定义常量的主要区别是什么? 从5.3版本 ...

  7. ABP数据库的迁移

    添加表,一(Test)对多(Test1)关系 Test using Abp.Domain.Entities.Auditing; using System; using System.Collectio ...

  8. 测试openssl_encrypt

    <?php //$string = 'It works ? Or not it works ?'; //$pass = '1234'; //$method = 'aes128'; // // / ...

  9. Codeforces 899B Months and Years

    题目大意 给定 $n$($1\le n\le 24$)个正整数 $a_1,\dots, a_n$ 判断 $a_1$ 到 $a_n$ 是否可能为连续 $n$ 个月份的天数. 解法 由于 $n\le 24 ...

  10. 阿狸的打字机(bzoj 2434)

    Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...