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. day06 面向对象编程

    面向对象: 面向对象: 世界万物,皆可分类 世界万物,皆为对象   只要是对象,就肯定属于某种品类 只要是对象,就肯定有属性         特性: 多态: 一个统一的接口,多种实现  例如:  一个 ...

  2. Oracle Flashback(flashback table或drop)

    在Oracle 10g中,Flash back家族分为以下成员:Flashback DatabaseFlashback DropFlashback TableFlashback Query(分Flas ...

  3. [POJ 1003] Hangover C++解题

        Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95164   Accepted: 46128 De ...

  4. const用法归纳总结 C++

    非常好的一篇分析const的总结归纳, 在此谢谢原作者:http://blog.csdn.net/zcf1002797280/article/details/7816977 在普通的非 const成员 ...

  5. Selenium WebDriver- 通过源码中的关键字找到我们要操作的句柄,用于多个窗口之间切换

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  6. SQL中使用关键词创建表或字段

    有时候我们给表或者字段命名时,会无意中选择了一个SQL中的关键字进行命名,然后就报错了: ERROR:  syntax error at or near "limit" MySQL ...

  7. 设计模式(十六)迭代器模式 Iterator

    什么时候需要用到迭代器模式? 有许多中方法,可以把对象堆起来放进一个集合(可以是数组.堆栈.列表.哈希表,等等). 每一种类型的集合,都有各自适用的时机.但是某个时间段,客户端可能希望去遍历这个集合. ...

  8. 基于2.9.6vue-cli初始化webpack工程

    前天做了组内的分享 讲了些webpack的东西 整个流程以及build文件夹内webpack相关的一些js所有的代码 每行代码什么意思 有什么作用 都做了很详细的标明. webpack是3.6的 今天 ...

  9. iOS----闪退,无报错原因,经典解决方案

    在iOS开发时,有时候遇到libc++abi.dylib handler threw exception这样的异常,  虽然在断点出加上了All Exceptions,也断到相应的代码了,但是没打印对 ...

  10. IB_DESIGNABLE 和 IBInspectable 的用法

    我们经常会在用一些自定义 UIView 来完成一些特殊的UI效果,但是怎么让我自定义的 UIView 在 Storyboard 中预览和修改一些自定义参数呢.这就需要用到两个吊吊的东西. IB_DES ...