插入排序算法_您需要了解的有关插入排序算法的所有信息

插入排序算法

介绍 (Introduction)

Hi! I am Sanjula, and in this guide I hope to teach you a little bit about the Insertion Sort algorithm including:

嗨! 我是Sanjula ,在本指南中,我希望教您一些有关插入排序算法的知识,包括:

  • What is Insertion sort?

    什么是插入排序?
  • Why is insertion sort important?

    为什么插入排序很重要?
  • Performance of Insertion Sort

    插入排序的性能
  • How does Insertion sort work?

    插入排序如何工作?
  • Java Implementation of Insertion sort

    Java实现的插入排序

Let’s get started!

让我们开始吧!

什么是插入排序? (What is Insertion sort?)

It is a simple sorting algorithm that sorts an array one item at a time.

这是一种简单的排序算法,可以一次将一个数组排序一次。

为什么插入排序很重要? (Why is insertion sort important?)

Insertion sort has several advantages including:

插入排序具有以下优点:

  • The pure simplicity of the algorithm.

    该算法的纯粹简单性。
  • The relative order of items with equal keys does not change.

    具有相同键的项的相对顺序不变。
  • The ability to sort a list as it is being received.

    接收列表时对列表进行排序的能力。
  • Efficient for small data sets, especially in practice than other quadratic algorithms — i.e. O(n²).

    对于小型数据集有效,特别是在实践中要比其他二次算法(即O(n²))有效。
  • It only requires a constant amount of additional memory space — O(1).

    它仅需要恒定数量的附加存储空间-O(1)。

插入排序的性能 (Performance of Insertion Sort)

  • Worst-case performance of insertion sort is O(n²) comparisons and swaps.

    插入排序的最坏情况是O(n²)比较和交换。
  • Best-case performance is O(n) comparisons and O(1) swaps.

    最佳情况下的性能是O(n)比较和O(1)交换。
  • Average-case performance is O(n²) comparisons and swaps.

    平均情况下的性能为O(n²)比较和交换。

插入排序如何工作? (How does Insertion sort work?)

In each iteration, insertion sort compares the current element with the next element and determines whether the current element is greater than the one it was compared to.

在每次迭代中,插入排序都会将当前元素与下一个元素进行比较,并确定当前元素是否大于被比较的元素。

If this is true, then it leaves the element in its place and moves on to the next element. If it is false, then it finds its correct position in the sorted array and moves it to that position by shifting all the elements which are larger in the sorted array to one position ahead.

如果为true ,则它将元素保留在其位置,然后移至下一个元素。 如果为false ,则它将在已排序数组中找到正确的位置,然后通过将已排序数组中较大的所有元素移到前面的一个位置,将其移动到该位置。

Java实现的插入排序 (Java Implementation of Insertion sort)

P.S. — Try to implement it on your own first!

PS —尝试首先自己实施!



Congratulations!!! You have now absorbed the basic but essential knowledge about how Insertion Sort works.

恭喜!!! 您现在已经掌握了有关插入排序工作原理的基本但必不可少的知识。

For reference or reporting issues regarding the code above, use the following public GitHub Gist link.

有关以上代码的参考或报告问题,请使用以下公共GitHub Gist 链接



Hope this was helpful. Thanks for reading! :)

希望这会有所帮助。 谢谢阅读! :)

翻译自: https://www.freecodecamp.org/news/everything-you-need-to-know-about-insertion-sort-algorithm/

插入排序算法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值