Big Theta 表示法和选择排序

发布于 2024-12-04 05:04:23 字数 328 浏览 1 评论 0原文

当数组通过重复附加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2024-12-11 05:04:23

升序,

  1. 找到列表中的最小值
  2. 将其与第一个位置的值交换 对
  3. 列表的其余部分重复上述步骤(从
    第二个位置并每次前进)

因此,我们必须检查所有元素的其余部分,无论风雨无阻,都必须通过检查(即使存在相同的元素)直到最后一个元素来获得最小值。

A - an array containing the list of numbers
numItems - the number of numbers in the list

for i = 0 to numItems - 1
    for  j = i+1 to numItems               
        if A[i] > A[j]
            // Swap the entries
            Temp = A[i]
            A[i] = A[j]
            A[j] = Temp          
        End If    
    Next j
Next i

正如您所看到的上面的伪代码,两个循环都在没有任何条件的情况下迭代到其限制的末尾。所以它给出了θ(n²)。然而,对于交换情况,O(n) 平均、θ(n) 最差和 θ(1) 最佳情况。

输入图片此处描述

Ascending order,

  1. Find the minimum value in the list
  2. Swap it with the value in the first position
  3. Repeat the steps above for the remainder of the list (starting at
    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.

A - an array containing the list of numbers
numItems - the number of numbers in the list

for i = 0 to numItems - 1
    for  j = i+1 to numItems               
        if A[i] > A[j]
            // Swap the entries
            Temp = A[i]
            A[i] = A[j]
            A[j] = Temp          
        End If    
    Next j
Next i

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.

enter image description here

无声无音无过去 2024-12-11 05:04:23

不会,这对效率没有影响。

选择排序中,由于嵌套循环,性能为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文