英文:
Can the best-case time complexity be the same as the worst case time complexity?
问题
因此,我被给予了这个算法:
CocktailShakerSort(A)
l = 1
r = n
while l < r do
for i = l to r - 1 do
if A[i] > A[i + 1] then
swap (A[i];A[i + 1])
end
end
r = r 􀀀 1
for i = r - 1 downto l do
if A[i] > A[i + 1] then
swap (A[i];A[i + 1])
end
end
l = l + 1
end
return A
我需要找到这个算法的最佳情况、最坏情况和平均情况时间复杂度。
理论上,最佳情况是数组已经排序好的情况。
我的问题是,这个算法的最佳情况时间复杂度是否与最坏情况相同,也就是O(n^2),因为它仍然必须完成前两个循环。
英文:
So I was given this algorithm :
CocktailShakerSort(A)
l = 1
r = n
while l < r do
for i = l to r - 1 do
if A[i] > A[i + 1] then
swap (A[i];A[i + 1])
end
end
r = r 􀀀 1
for i = r - 1 downto l do
if A[i] > A[i + 1] then
swap (A[i];A[i + 1])
end
end
l = l + 1
end
return A
and I need to find the Best-Case, Worst-Case and Average-Case Time Complexity.
Theoretically, the Best-Case is the case in which the array is already sorted.
My question is if the best case time complexity in this algorithm is the same as the worst case meaning 0(n^2), since it still has to complete the first two loops.
答案1
得分: 0
首先,欢迎来到 Stack Overflow。
该算法的最佳情况时间复杂度与最坏情况相同,即 O(n^2),因为它仍然必须完成前两个循环。
简而言之,是的,这个算法的最佳和最坏时间复杂度都是 O(n^2)。
最佳情况时间复杂度是否可以与最坏情况时间复杂度相同?
是的,有很多情况。例如,选择排序具有 O(n^2) 和 Ω(n^2)。你可以在这里查看更多。
从理论上讲,最佳情况是数组已经排序好的情况。
正如你所说,在最佳情况下(即已排序的数组),两个循环仍然会在每次外部迭代中运行,进行比较,而不执行任何交换。因此,仍然执行 O(n^2) 次操作。尽管如果有一个变量来检查迭代中是否进行了任何交换,时间复杂度可以减少到 O(n)。也就是说,在第一个循环的第一次迭代结束时,我们检查:
//其他循环
//内部循环1
if (bSwaps == false)
return A
//内部循环2
解释:
从我对这段代码的理解来看,外部循环运行 n-1 次,第一个内部循环看起来类似于冒泡排序,而第二个内部循环则执行第一个循环的相反操作。
因此,对于外部循环的第一次迭代,操作数量将是
=> K1 * (n-1) + K2 * (n-2)
其中,k1/k2 是两个循环执行的一些常数操作。
因此,对于 n 次迭代,操作数量将是:*K * 从1到 n-1 的 n 的总和
=> K * (n^2 - n) / 2 => O(n^2)
英文:
First of All welcome to stack overflow.
>best case time complexity in this algorithm is the same as the worst case meaning 0(n^2), since it still has to complete the first two loops
In short Yes, Best & worst T.C (Time complexity) of this algorithm is O(n^2).
>Can the best-case time complexity be the same as the worst case time complexity?
Yes, there are many. For e.g. Selection sort with O(n^2) & Ω(n^2). You can check more here.
>Theoretically, the Best-Case is the case in which the array is already sorted.
As you have said, in best case (i.e. sorted array) the two loops will still run for each external iteration, doing comparison, without performing any swaps. Thus still performing O(n^2) operations.
Although if there has been a variable to check whether any swapping was done in the iteration, T.C could be reduced to O(n). i.e. At the end of first iteration of first loop we check
//Other loop
//Inner loop 1
if (bSwaps == false)
return A
//Inner loop2
Explanation:
From what I can understand from this code, outer loop runs n-1 times and first inner loop looks similar to that of bubble sort and second inner loop does the reverse of the first loop.
So for first iteration of outer loop, no. of operation would be
=> K1 * (n-1) + K2 * (n-2)
where, k1/k2 are some constant operations performed by 2 loops.
Therefore, for n iteration, no. of operation would be: *K * summation of n from 1 to n-1
=> K * (n^2 - n) / 2 => O(n^2)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论