Big Theta 表示法和选择排序
当数组通过重复附加 19 来增长时,选择排序算法的 Big-Theta (T) 表示法的最佳情况和最坏情况复杂度是多少?
例如:
[ 19, 13, 7, 19, 12, 16, 19 ],
[ 19, 13, 7, 19, 12, 16, 19, 19 ],
[ 19, 13, 7, 19, 12, 16, 19, 19, 19 ]
等等。 n
用于表示数组的长度。
因此,我们将相同的数字添加到数组的末尾,但这个数字也恰好是最大的数字,因此它将保留在数组的末尾。这是否意味着它真的对效率没有任何影响?我很困惑。
What would be the best-case and worst-case complexity in Big-Theta (T) notation of the selection sort algorithm when the array grows by repeatedly appending a 19?
For instance:
[ 19, 13, 7, 19, 12, 16, 19 ],
[ 19, 13, 7, 19, 12, 16, 19, 19 ],
[ 19, 13, 7, 19, 12, 16, 19, 19, 19 ]
Et cetera. n
is used to represent the length of the array.
So we're adding the same number to the end of the array, but this number also happens to be the largest number so it would stay at the end of the array. Does this mean that it really doesn't have any affect on the efficiency? I'm very confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
升序,
第二个位置并每次前进)
因此,我们必须检查所有元素的其余部分,无论风雨无阻,都必须通过检查(即使存在相同的元素)直到最后一个元素来获得最小值。
正如您所看到的上面的伪代码,两个循环都在没有任何条件的情况下迭代到其限制的末尾。所以它给出了
θ(n²)
。然而,对于交换情况,O(n)
平均、θ(n)
最差和θ(1)
最佳情况。Ascending order,
the second position and advancing each time)
So, we have to examine the rest of all elements rain or shine to obtain the minimum by checking, even if same elements are there, up to the last element.
As you can see the above pseudo-code, both loops iterate up to end of their limits without any condition. So it gives
θ(n²)
. However,O(n)
average,θ(n)
worst andθ(1)
best cases for swap circumstance.不会,这对效率没有影响。
在选择排序中,由于嵌套循环,性能为N^2。即使末尾的元素不需要交换,循环仍然会比较它们。
所以,要回答你的确切问题:由于最好情况、最坏情况和平均情况性能都是 N^2,并且对效率没有影响,所以性能没有变化。
No, it has no effect on the efficiency.
In selection sort, the performance is N^2 because of the nested loops. Even though the elements at the end do not need to be swapped, the loops will still compare them.
So, to answer your exact question: since the best case, worst case, and average case performance are all N^2, and there is no effect on efficiency, there is no change in the performance.