问题 c: 插入排序_插入排序:它是什么,以及它如何工作
问题 c: 插入排序
Insertion sort is a simple sorting algorithm for a small number of elements.
插入排序是一种针对少量元素的简单排序算法。
例: (Example:)
In Insertion sort, you compare the key
element with the previous elements. If the previous elements are greater than the key
element, then you move the previous element to the next position.
在插入排序中,您将key
元素与之前的元素进行比较。 如果先前的元素大于key
元素,则将先前的元素移动到下一个位置。
Start from index 1 to size of the input array.
从索引1开始到输入数组的大小。
[ 8 3 5 1 4 2 ]
[8 3 5 1 4 2]
Step 1 :
第1步 :
key = 3 //starting from 1st index.Here `key` will be compared with the previous elements.In this case, `key` is compared with 8. since 8 > 3, move the element 8to the next position and insert `key` to the previous position.Result: [ 3 8 5 1 4 2 ]
Step 2 :
第2步 :
key = 5 //2nd index8 > 5 //move 8 to 2nd index and insert 5 to the 1st index.Result: [ 3 5 8 1 4 2 ]
Step 3 :
第三步:
key = 1 //3rd index8 > 1 => [ 3 5 1 8 4 2 ] 5 > 1 => [ 3 1 5 8 4 2 ]3 > 1 => [ 1 3 5 8 4 2 ]Result: [ 1 3 5 8 4 2 ]
Step 4 :
第4步 :
key = 4 //4th index8 > 4 => [ 1 3 5 4 8 2 ]5 > 4 => [ 1 3 4 5 8 2 ]3 > 4 ≠> stopResult: [ 1 3 4 5 8 2 ]
Step 5 :
步骤5:
key = 2 //5th index8 > 2 => [ 1 3 4 5 2 8 ]5 > 2 => [ 1 3 4 2 5 8 ]4 > 2 => [ 1 3 2 4 5 8 ]3 > 2 => [ 1 2 3 4 5 8 ]1 > 2 ≠> stopResult: [1 2 3 4 5 8]
The algorithm shown below is a slightly optimized version to avoid swapping the key
element in every iteration. Here, the key
element will be swapped at the end of the iteration (step).
下面显示的算法是经过稍微优化的版本,可避免在每次迭代中交换key
元素。 此处, key
元素将在迭代(步骤)结束时交换。
InsertionSort(arr[])for j = 1 to arr.lengthkey = arr[j]i = j - 1while i > 0 and arr[i] > keyarr[i+1] = arr[i]i = i - 1arr[i+1] = key
Here is a detailed implementation in JavaScript:
这是JavaScript的详细实现:
function insertion_sort(A) {var len = array_length(A);var i = 1;while (i < len) {var x = A[i];var j = i - 1;while (j >= 0 && A[j] > x) {A[j + 1] = A[j];j = j - 1;}A[j+1] = x;i = i + 1;}
}
A quick implementation in Swift is shown below :
Swift中的快速实现如下所示:
var array = [8, 3, 5, 1, 4, 2]func insertionSort(array:inout Array<Int>) -> Array<Int>{for j in 0..<array.count {let key = array[j]var i = j-1while (i > 0 && array[i] > key){array[i+1] = array[i]i = i-1}array[i+1] = key}return array}
The Java example is shown below:
Java示例如下所示:
public int[] insertionSort(int[] arr)for (j = 1; j < arr.length; j++) {int key = arr[j]int i = j - 1while (i > 0 and arr[i] > key) {arr[i+1] = arr[i]i -= 1}arr[i+1] = key}return arr;
插入排序在... (insertion sort in c....)
void insertionSort(int arr[], int n)
{ int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i-1;while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; }
}
特性: (Properties:)
- Space Complexity: O(1)空间复杂度:O(1)
Time Complexity: O(n), O(n* n), O(n* n) for Best, Average, Worst cases respectively.
时间复杂度:分别为最佳,平均和最差情况下的O(n),O(n * n),O(n * n)。
- Best Case: array is already sorted最佳情况:数组已排序
- Average Case: array is randomly sorted平均情况:数组随机排序
- Worst Case: array is reversely sorted.最坏的情况:数组被反向排序。
- Sorting In Place: Yes就地排序:是
- Stable: Yes稳定:是的
其他资源: (Other Resources:)
Wikipedia
维基百科
CS50 - YouTube
CS50-YouTube
SortInsertion - GeeksforGeeks, YouTube
SortInsertion-GeeksforGeeks,YouTube
Insertion Sort Visualization
插入排序可视化
Insertion Sort - MyCodeSchool
插入排序-MyCodeSchool
Insertion Sort - VisuAlgo
插入排序-VisuAlgo
翻译自: https://www.freecodecamp.org/news/insertion-sort-what-it-is-and-how-it-works/
问题 c: 插入排序
相关文章:

在Java连接hbase时出现的问题
问题1: java.net.ConnectException: Connection refused: no further information zookeeper.ClientCnxn: Session 0x0 for server null zookeeper未启动,或无法连接,从查看各节点zookeeper启动状态、端口占用、防火墙等方面查看原因。问题2&…

codeforces 8C. Looking for Order 状压dp
题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动。 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程。 直接状压dp就好了。 #include <iostream> #include <vector> #…

H5刷新当前页面
location.reload();

sql的外键约束和主键约束_SQL主键约束用示例解释
sql的外键约束和主键约束A primary key is a column or a set of columns that uniquely identifies each row in a table.主键是一列或一组列,它们唯一地标识表中的每一行。 It’s called a “constraint” because it causes the system to restrict the data al…

str.format() 格式化字符串函数
语法 它通过{}和:来代替%。 “映射”示例 通过位置 In [1]: {0},{1}.format(kzc,18) Out[1]: kzc,18 In [2]: {},{}.format(kzc,18) Out[2]: kzc,18 In [3]: {1},{0},{1}.format(kzc,18) Out[3]: 18,kzc,18字符串的format函数可以接受不限个参数,位置可以…

css学习任务二:切图写代码
今天的任务是根据UI给的图进行切图,然后写出相应的页面,UI如下: 收获:学习前端知识一年有余,却因为老是找不到实战项目而得不到实际的提高,直到今天的学习我才知道切图是怎么一回事,明白了你看到…

Vue mixins(混入) 附代码示例详解
mixins 我们称它为 “混入” ; 官方的解释: 混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能。一个混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被“混合”进入该组件本身的…

软件开发面试_如何为成功的软件开发工作面试做准备
软件开发面试Job interviews are stressful for many people. Besides the pressure of getting hired, you have to answer various questions before and during the interview – like what to wear, how to get prepared, how much money to ask for, and much more.求职面…

bzoj1070————2016——3——14
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id1070; 题目概括: Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心。维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的。现…

CSS兼容性汇总
http://www.jb51.net/css/469020.html CSS属性Hack 把属性hack分为 前缀属性hack和 后缀属性hack CSS属性Hack(前缀)针对的浏览器_color:red;IE6及其以下的版本*color:red ;或者 color:red;IE7及其以下的版本CSS属性Hack(后缀)针对…

Vue 过渡组件,可实现组件或者页面的动画过渡或者css过渡
使用过渡效果,可以优化用户体验,Vue给我们封装了一个很好用的组件,专门用来处理过渡效果,下面我们来看看怎么使用它; Vue 提供了 transition 的封装组件,在下列情形中,可以给任何元素和组件添加…

解释型和编译型编程语言_解释型和编译型编程语言:有什么区别?
解释型和编译型编程语言Every program is a set of instructions, whether it’s to add two numbers or send a request over the internet. Compilers and interpreters take human-readable code and convert it to computer-readable machine code. 每个程序都是一组指令&a…

Beta 冲刺 (1/7)
队名:天机组 组员1友林 228(组长) 今日完成:查找了相关资料及api文档。明天计划:继续相关资料及源码。剩余任务:优化网络通讯机制主要困难:查找的代码调试较为困难。收获及疑问:暂无…

Vue全局路由侦听beforeEach路由守卫附代码使用示例
使用路由守卫beforeEach,可以实现路由侦听; 全局侦听路由跳转的实现代码: app.vue onLaunch: function(e) {this.$router.beforeEach((to, from, next) > {console.log($router,to,from);next();}); } to 是跳转路由之后的page对象&am…

Debug模式下加载文件,运行程序异常的慢
今天在进行单元测试的时候,debug模式下加载速度很慢,但是run模式下速度很快。 原因:在debug模式下,断点位置不当,解决办法 移除编译器中的所有断点。转载于:https://www.cnblogs.com/nww57/p/5277113.html

如何在Python中对字符串进行子字符串化
Python offers many ways to substring a string. It is often called ‘slicing’.Python提供了许多对字符串进行子字符串化的方法。 它通常被称为“切片”。 It follows this template:它遵循以下模板: string[start: end: step]Where,哪里, start:…

HttpPost导包遇到的问题
直接在当前项目 build.gradle文件修改如下 android { useLibrary org.apache.http.legacy compileSdkVersion 24 buildToolsVersion "24.0.0" defaultConfig { applicationId "com.ican.subjects" minSdkVe…

真相也许是这样
这两天同学们陆续上传了自己编写的小程序,等老师审查给成绩的时候才发现一部分同学是负分。原因就是他们有抄袭之嫌。恰巧我当时就在一位得了负分的同学旁边,他一脸郁闷的对我说”没道理啊,这是我自己编的啊“。这个我知道,的确是…

微信小程序全局监听路由变化
小程序有一个API可以侦听全局路由跳转,官方文档里面没有但是可以使用。 wx.onAppRoute((res) > { console.log(路由监听,{res}) })

c语言面向对象编程中的类_C ++中的面向对象编程
c语言面向对象编程中的类Object oriented programming, OOP for short, aims to implement real world entities like inheritance, hiding and polymorphism in programming. 面向对象的编程,简称OOP,旨在在编程中实现诸如继承,隐藏和多态性…

Maven build标签
前言: <build >设置,主要用于编译设置 1.分类 在Maven的pom.xml文件中,存在如下两种<build>: (1)全局配置(project build) 针对整个项目的所有情况都有效 (2…

Ant Design Vue 表格内编辑(附完整源码及效果图)
效果图: 实现关键代码就是表单的 columns 属性对象下标的 scopedSlots: scopedSlots: {customRender: } 实现完整代码: <template><div><div class"table-wrapper"><!--每个列的宽度必须比列名总长度大才能…

shell基本用法
#!/bin/bash #使用哪个shell执行echo "hello world" username"tom" echo $username#chmod x ./test.sh 设置可以执行文件#for循环输出 for skill in Ada Coffe Action Java; doecho "I am good at ${skill}Script" done #for file in ls /etc; d…

brad wu_一百万归功于Brad Traversy
brad wuBrad Traversy is one of the most appreciated web development teachers on YouTube and he is getting close to 1 Million subscribers. We decided to help him reach this goal by the end of 2019.布拉德特拉弗西 ( Brad Traversy)是YouTube上最受赞赏的网络开发…

CSS常见布局解决方案
最近要准备移动端项目,大半年没好好写过CSS了,今天恶补了一下CSS的一些布局,下面做一些分享。 水平居中布局 1.margin 定宽 <div class"parent"><div class"child">Demo</div> </div><style…

java.io.EOFException java.io.ObjectInputStream$PeekInputStream.readFully 错误
omcat 启动时报以下错误: java.io.EOFException at java.io.ObjectInputStream$PeekInputStream.readFully 错误 这个错误 碰到好几次了,我的tomcat使用非常频繁,而且部署项目比较多,经常会出一些自己看不懂的问题, 今天解决了这…

SQL替换字段中部分字符
示例: tb_item表中image字段中一数据为:jd/4ef8861cf6854de9889f3db9b24dc371.jpg update tb_item set image replace(image,jd,taobao); 替换后: taobao/4ef8861cf6854de9889f3db9b24dc371.jpg

flexbox_Flexbox中的Flex基础属性
flexbox弹性基础 (Flex Basis) The flex-basis property defines the size of the flex-item along the main axis of the flex container. The main axis is horizontal if flex-direction is set to row and it’ll be vertical if the flex-direction property is set to co…

OpenStack之虚拟机热迁移
这里的环境是centos7版本,openstack K版 1.在各个计算节点设置权限 chmod 755 /var/lib/nova/instances 2.修改各个节点的nova.conf(/etc/nova/nova.conf) vncserver_proxyclient_address虚拟机IP # vncserver_listen0.0.0.0 3.修改所有计算节点libvirt 3.1 修改/e…

软件工程概论个人作业02
可怜的二柱子同学,老师又对他的自动出题系统提出了新的要求: 1、题目避免重复; 2、可定制(数量/打印方式); 3、可以控制下列参数: 是否有乘除法; 是否有括号(最多可以支持十个数参与计算)&#…