问题
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