简体   繁体   English

排序算法的上界和下界

[英]Upper bound and lower bound of sorting algorithm

This is a very simple question but I'm struggling too much to understand the concept completely. 这是一个非常简单的问题,但我正在努力完全理解这个概念。

I'm trying to understand the difference between the following statements: 我试图理解以下陈述之间的区别:

  1. There exists an algorithm which sorts an array of n numbers in O(n) in the best case. 存在一种算法,其在最佳情况下对O(n)中的n个数组进行排序。
  2. Every algorithm sorts an array of n numbers in O(n) in the best case. 在最佳情况下,每个算法都在O(n)中对n个数字的数组进行排序。
  3. There exists an algorithm which sorts an array of n numbers in Omega(n) in the best case. 存在一种算法,其在最佳情况下对Omega(n)中的n个数组进行排序。
  4. Every algorithm sorts an array of n numbers in Omega(n) in the best case. 在最佳情况下,每个算法都在Omega(n)中对n个数字的数组进行排序。

I will first explain what is driving me crazy. 我将首先解释是什么让我发疯。 I'm not sure regarding 1 and 3 - but I know that for one of them the answer is correct just by specifying one case and for the other one the answer is correct by examining all the possible inputs. 我不确定1和3 - 但我知道其中一个答案是正确的,只需指定一个案例,而另一个案例答案是正确的,通过检查所有可能的输入。 Therefore I know one of them must be true just by specifying that the array is already sorted but I can't tell which. 因此,我知道其中一个必须是真的,只需指定数组已经排序但我无法分辨哪个。 My teacher always told me to think about it like we are examining who's the heighest guy in the class and again by one of these options(1,3) it's enough to say that he is and there is no reason to examine all the class. 我的老师总是让我思考这个问题就像我们正在研究谁是班上最高级的人,再次选择其中一个选项(1,3)就足以说明他是,并且没有理由去检查所有班级。

I do know that if we were to examine the worst case then none of these statements could be true because the best sorting algorithm without any assumptions or additional memory is Omega(nlogn) . 我知道如果我们要检查最坏的情况,那么这些陈述都不可能是真的,因为没有任何假设或额外记忆的最佳排序算法是Omega(nlogn)

IMPORTANT NOTE: I'm not looking for a solution (an algorithm which is able to do the matching sort) - only trying to understand the concept a little better. 重要说明:我不是在寻找一种解决方案(一种能够进行匹配排序的算法) - 只是试图更好地理解这个概念。

Thank you! 谢谢!

For 1+3 ask yourself - do you know an algorithm that can sort an array at best case in Theta(n) - if the answer is true, then both 1+3 are true - since Theta(n) is O(n) [intersection] Omega(n) , and thus if you do have such an algorithm (that runs in Theta(n) best case) - both 1+3 are correct. 对于1 + 3问你自己 - 你知道一个算法可以在Theta(n)最佳情况下对数组进行排序 - 如果答案是真的,那么1 + 3都是真的 - 因为Theta(n)是O(n) [intersection] Omega(n) ,因此如果你有这样的算法(以Theta(n)最佳情况运行) - 1 + 3都是正确的。
Hint: optimized bubble sort . 提示:优化的冒泡排序

For 2 : ask yourself - does EVERY algorithm sorts an array of numbers in O(n) best case? 对于2 :问自己 - 每个算法是否在O(n)最佳情况下对一组数字进行排序? Do you know an algorithm that have a worst case and best case identical time complexity? 您是否知道具有最差情况和最佳情况相同时间复杂度的算法? What happens to the mentioned bubble sort if you take all optimizations off? 如果你取消所有优化,上述冒泡排序会发生什么?

For 4 : ask yourself - do you need to read all elements in order to ensure the array is sorted? 4 :问问自己 - 你需要阅读所有元素以确保数组排序吗? If you do - Omega(n) is a definite lower bound, you cannot go better then it. 如果你这样做 - Omega(n)是一个确定的下限,你就不能比它更好。

Good Luck! 祝好运!

The difference, obviously, is in terms "O" and "Omega". 显然,差异在于“O”和“Omega”。 One says "rising not faster than", second says "rising not slower than". 一个人说“上升不快”,第二个说“上升不慢”。

Make sure that you understand the difference between those terms, and you'll see the difference in the sentences. 确保您了解这些术语之间的区别,并且您将看到句子中的差异。

1 and 3 both state completely different things, just as 2 and 4 are. 1和3都表示完全不同的东西,就像2和4一样。

Look at those (those are NOT the same!): 看看那些(那些不一样!):

1~ there exists an algorithm that for 10 items doesn't take more than 30 in the best case. 1~存在一种算法,对于10个项目,在最佳情况下不超过30个。
3~ there exists an algorithm that for 10 items doesn't take less than 30 in the best case. 3~存在一种算法,在最佳情况下10项不小于30。

2~ every algorithm that for 10 items takes not more than 30 in the best case. 2~在10种情况下,每种算法在最佳情况下不超过30。
4~ every algorithm that for 10 items takes not less than 30 in the best case. 4~最好的情况下10个项目的每个算法不少于30个。

Do you sense the difference now? 你现在感觉到了区别吗? With O/Omega the difference is similar, but the subject of investigation differs. 与O / Omega的区别相似,但调查的主题不同。 The examples above say about different performance in some point/case, while O/Omega notation tell you about the performance, related to the size of data, but only if the data "is large enough", be it three items or milions, and it drops constant factors: 上面的例子说明了某些点/情况下的不同性能,而O / Omega表示法告诉你与数据大小相关的性能,但只有当数据“足够大”时,无论是三项还是数百万,它会降低常数因素:

function 1000000*n is O(n)
function 0.00000*n*n is O(n^2)

For small amounts data, second one is obviously very very better than first. 对于少量数据,第二个显然非常好于第一个。 But as the quantity of data rises, soon the first starts to be much better! 但随着数据量的增加,第一次开始变得更好!

Rewriting the above examples into "more proper" terms, that are more similar to your original sentences: 将上述示例重写为“更合适”的术语,与原始句子更相似:

1~ there exists an algorithm that, for more than N items, doesn't take more than X*N in the best case. 1~存在一种算法,对于多于N个项目,在最佳情况下不超过X * N.
3~ there exists an algorithm that, for more than N items, doesn't take less than X*n in the best case. 3~存在一种算法,对于多于N个项目,在最佳情况下不小于X * n。

2~ every algorithm that, for more than N items, takes not more than X*N in the best case. 2~每个算法,对于N个以上的项目,在最佳情况下不超过X * N.
4~ every algorithm that, for more than N items, takes not less than X*N in the best case. 4~每个算法,对于N个以上的项目,在最佳情况下不小于X * N.

I hope that this helps you with "seeing"/"feeling" the difference! 我希望这能帮助你“看到”/“感受”差异!

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

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