问题 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: 插入排序

问题 c: 插入排序_插入排序:它是什么,以及它如何工作相关推荐

  1. java sort 插入排序_插入排序(insertSort)

    插入排序,在数组中,保证 从位置0 到 p 位置 配上的元素都是已排序状态.每次比较 p+1 位置上的元素和已排序的列表,将 p+1 位置上的元素,加入到已排序的列表中. /** * created ...

  2. java 插入排序_看动画学算法之:排序-插入排序

    简介 插入排序就是将要排序的元素插入到已经排序的数组中,从而形成一个新的排好序的数组. 这个算法就叫做插入排序. 插入排序的例子 同样的,假如我们有一个数组:29,10,14,37,20,25,44, ...

  3. c语言插入排序_还有这种操作?C语言插入排序算法,一点就透

    插入排序算法是所有排序方法中最简单的一种算法,其主要的实现思想是将数据按照一定的顺序一个一个的插入到有序的表中,最终得到的序列就是已经排序好的数据. 更多C/C++资料群文件:569268376 直接 ...

  4. 第四周 Java语法总结_内部类_常用类_getclass_toStrong_equals_clone_Scanner_String_StringBuffer_Integer_集合_插入排序

    文章目录 20.内部类 1.格式 2.外部类访问内部类 3.局部内部类 4.匿名内部类 1)方法的形式参数是接口 2)关于匿名内部类在开发中的使用 3)方法的返回值是接口或抽象类型 16.常用类 1. ...

  5. 王道八大排序:直接插入排序 折半插入排序 希尔排序 冒泡排序 快速排序 归并排序 基数排序

    文章目录 1.插入排序 1.1直接插入排序 1.2折半插入排序 1.3希尔排序 2.交换排序 2.1冒泡排序 2.2快速排序 3.选择排序 3.1简单选择排序 3.2堆排序 4.归并排序 5.基数排序 ...

  6. C语言——十四种内部排序算法【直接插入排序-冒泡排序-选择排序-插入排序-希尔排序-归并排序-快速排序-堆排序-折半插入排序-二分查找-路插入排序-表插入排序-简单选择排序-直接选择排序-树形选择】

    目录: 一:插入排序 A:直接插入排序 1.定义: 2.算法演示 实例1: 3.基本思想 4.排序流程图 实例1: B:希尔排序 1.定义: 2.算法演示 实例2: C:其他插入排序 a:折半插入排序 ...

  7. Disruptor本地线程队列_实现线程间通信---线程间通信工作笔记001

    Disruptor本地线程队列_实现线程间通信---线程间通信工作笔记001 看到同事用这个东西了,这个挺好用的说是,可以实现,本地线程间的通信,好像在c++和java中都可以用 现在没时间研究啊,暂 ...

  8. java 折半插入排序_[Java代码] Java实现直接插入排序和折半插入排序算法示例

    1 排序思想: 将待排序的记录Ri插入到已经排好序的记录R1,R2,--,R(N-1)中. 对于一个随机序列而言,就是从第二个元素开始,依次将这个元素插入到它之前的元素中的相应位置.它之前的元素已经排 ...

  9. 上标3下标6算法_插入排序算法导学案

    本文为"选考VB算法专题系列讲座9插入排序算法"视频配套的导学案,请同学们先完成导学案再收看视频.明天将推送"选考VB算法专题系列讲座9插入排序算法"视频,敬请 ...

最新文章

  1. 解决 复制虚拟机无法上网 看不到IP地址
  2. linux resolv.conf详解
  3. 快捷键设置_win10自带截图工具如何使用 、设置快捷键
  4. 关于小程序取data- 的值的问题
  5. C和指针之实现可变参数函数编译出现expanded from macro ‘va_arg‘ #define va_arg(ap, type) 解决办法
  6. Finally 与 return
  7. 疑似OPPO Find X2外观专利曝光:月亮形相机模组亮了
  8. centos7安装Nginx 配置及反向代理
  9. GLCM opencv
  10. h5页面如何切图_前端切图H5/网页切图/移动自适应H5/切图开发/H5响应式
  11. WIN10 PDF不显示缩略图 解决办法(修复工具下载)
  12. 孝敬老人用 盐城 淮剧视频 淮剧 王樵楼磨豆腐
  13. ktv收银管理系统服务器,KTV收银管理软件
  14. python离线录音转文字软件_语音转文字工具(音频转文字助手)V2.1 最新版
  15. linux双网卡连不上网,linux 双网卡配置问题
  16. 7个简单步骤解释区块链挖掘和交易如何工作
  17. Android 属性动画使用(二)
  18. 在国内用Windows给BT做种,真是一山绕过一山缠(附解决方案)
  19. ArcGis for js 查询定位、缩放致
  20. 用Postman测试网页接口

热门文章

  1. boost安装_【环境搭建】源码安装Boost
  2. jQuery绑定事件的三种常见方式(bind、one、【change、click、keydown、hover】)
  3. c# 读hex_c#十六进制到位转换(c# hex to bit conversion)
  4. js插件 webp_(转)让浏览器支持Webp
  5. vue注册新节点_vue怎么重新组装slots节点
  6. React for循环渲染组件
  7. Flex布局教程(来源:阮一峰)
  8. Java泛型:泛型类、泛型接口和泛型方法
  9. 6-5-树的双亲表示法-树和二叉树-第6章-《数据结构》课本源码-严蔚敏吴伟民版...
  10. 【哲学百科】文艺复兴及唯理主义时期(公元1500~公元1750)