简体   繁体   English

插入排序的运行时复杂度的最佳描述是什么?

[英]What is the best description of the run time complexity of insertion sort

I have understood it to be quadratic, but I am taking a multiple choice practice test for discrete math and the only four options are: 我知道它是二次方的,但是我正在对离散数学进行多项选择练习测试,并且仅有的四个选项是:

a) logarithmic a)对数

b) linear b)线性

c)linearithmic c)线性

d)polynomial d)多项式

a) logarithmic = O(log n) a)对数= O(log n)

b) linear = O(n) b)线性= O(n)

c) linearithmic = O(n log n) c)线性= O(n log n)

d) polynomial = O(n k ) d)多项式= O(n k

So O(n 2 ) would be polynomial. 因此O(n 2 )将是多项式。

Inserting a new value into a list or tree of sorted values is O(log n). 将新值插入到排序值列表或树中是O(log n)。

Easiest to see with a straight sorted list. 使用简单的排序列表最容易看到。 Lets say you want to insert x. 假设您要插入x。 Find the value at [n/2]. 找到[n / 2]处的值。 If this is greater than x, then the correct location lies on the left half. 如果该值大于x,则正确的位置位于左半部分。 If [n/2] is less than x, then the correct location lies on the right. 如果[n / 2]小于x,则正确的位置在右侧。 Repeat, checking the middle value each time. 重复,每次检查中间值。 Each step halves the size of the interval to check until you find the correct value, giving logarithmic time for the insertion. 每个步骤将检查间隔的大小减半,直到找到正确的值为止,然后给出插入的对数时间。

An insertion into a tree is similar; 插入树是类似的; you have to traverse the tree to the correct node position, which requires traversing the average depth of the tree, which is proportional to log n 您必须将树遍历到正确的节点位置,这需要遍历树的平均深度,该深度与log n成正比

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM