Prove that an algorithm has a lower bound

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 05:48:15

问题


I'm trying to prove this problem:

if an algorithm exists that can determine if a sorted list of n elements has duplicate elements in it, than the number of comparisons needed has a lower bound of n-1.

I'm not quite familiar with lower and higher bounds and I seem to confuse it, can someone help me with an easy to understand proof?


回答1:


The problem statement is not rigorous. It should say "the number of comparisons in the worst case".

In a sorted array, there are n-1 relations between pairs of successive elements, which are either < or =. If all elements are different, you cannot deduce the outcome of a comparison from that of other comparisons. Hence you cannot avoid an exhaustive search, taking up to n-1 tests.


By the way, n-1 is also an upper bound on the worst case, as after the exhaustive search you always have the answer.


In the best case, when the first two elements are equal, you find the answer after exactly 1 comparison. Hence, lower and upper bounds on the best case are both 1.




回答2:


If you have the array list = {1, 2, 3, 4, 5}, you have to compare the 1 and the 2, the 2 and the 3, the 3 and the 4 and so on, to determine if it has any duplicates in it.

So the total number of comparisons is 4 and the total number of elements is 5 and thus n-1 comparisons are needed.



来源:https://stackoverflow.com/questions/58820568/prove-that-an-algorithm-has-a-lower-bound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!