[Algorithms] Sort an Array with a Nested for Loop using Insertion Sort in JavaScript
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的更多相关文章
- 洛谷 SP9722 CODESPTB - Insertion Sort
洛谷 SP9722 CODESPTB - Insertion Sort 洛谷传送门 题目描述 Insertion Sort is a classical sorting technique. One ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
- [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. 这道题 ...
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...
- [Algorithms] Insertion sort algorithm using TypeScript
Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...
- 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 ...
- [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 ...
随机推荐
- luogu1233 木棍加工
先排个序然后做最长上升子序列就行了. #include <algorithm> #include <iostream> #include <cstdio> usin ...
- 两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js
估计网上能查到的最多的两种图片延迟加载方法就是jquery.scrollLoading.js与jquery.lazyload.js了,其中jquery.lazyload.js的调用方法因为有网友爆出的 ...
- debug环境下打印
#ifdef DEBUG # define NSLog(...) NSLog(__VA_ARGS__) #else # define NSLog(...) {} #endif
- webdriver高级应用- 使用日志模块记录测试过程中的信息
在自动化脚本执行过程中,使用Python的日志模块记录在测试用例执行过程中一些重要信息或者错误日志等,用于监控和后续调试脚本. 在pycharm下新建工程,并创建Log.py.Logger.conf以 ...
- 关于EF调用存储过程那点事...
最近研究了下EF怎么调用 数据库的分页存储过程,发现还是很不错的 1.数据库存储过程如下,一个简单的不含条件判断的 2.然后新建数据模型中选择存储过程: : 3.EF会自动生存一个返回复杂类型(Obj ...
- [python学习篇][python工具使用篇][1] 编辑,设置等
1 添加sublime到环境变量 win +r ,输入sysdm.cpl, 在弹出的界面选择高级,选择环境变量,编辑path,添加sublime的安装目录(这是sublime的一种安装方式,另外一种安 ...
- Response.End报错
以下摘抄自博问:https://q.cnblogs.com/q/31506/ try catch中使用Response.End() 我在WebForm中用ajax发送请求到页面index. ...
- 浅谈android反调试之轮询TracePid(解决方案是特色)
参考文章: 1. http://bbs.pediy.com/thread-207538.htm 2. http://www.wjdiankong.cn/android 需求: 常见的Android ...
- Eclipse项目类型转换
例如,将一个普通java项目改为动态Web项目: 在eclipse的项目上点右键,刷新项目. 在项目上点右键,进入属性(properties) 在左侧列表项目中点击选择“Project Facets” ...
- Wmap5 测试80端口 Your port 80 is actually used by :Server: Microsoft-HTTPAPI/2.0
问题:win7系统! 在wamp5的apache启动不了: 目录下点击[测试80端口]的时候提示:Your port 80 is actually used by : Server: Microsof ...