You obviously don't understand what O(1) means.
Let's see. The definition of big O is:
f(n) = O (g (n)) iff there are a M > 0 and a c > 0 such that
for all m > M, 0 <= f(m) <= c * g (m). [1] [
+2] [3]
I don't have any problem understanding with it. In layman terms, it
means that a function
f of
n is in the order of
g of
n, if, and only if, there's a constant, such that
if
n gets large enough, the value of
f is at most
the value of
g times said constant.
Search from beginning to end, going thru each element one by one, until
hit what you are searching for. In the worst case (the element is at the
end of the array), you have to hit 1 billion elements, but according to
you, that's O(1). I say it is O(n). We never put a restriction saying
that an array can at most contain 1 billion elements (so the size of
an array in general is not a constant, although it is a constant for a
given array at one given observation point.)
Hello? We never put a restriction on the size? Come again. What do you call:
And still O(1) is not reachable, unless each element resolve a unique key ;-)
That's a restriction of 1.
You started out by putting
restrictions on it, claiming that only if there's a restriction of a size
of 1, the search algorithm is O (1). I on the other hand pointed out that
as long as there is a restriction on the limit of the chain, it doesn't
matter what the restriction is, 1, 14 (for 5.8.2), or a billion. If there's
a restriction on the size, even with a linear search it's O (1). Here's
a proof:
Suppose the chain is limited to length K, where K is a constant, independent
of the amount of keys in the hash. Searching for a key is a two step
process: first we need to find the bucket the key hashes to, then we need
to find the key in the associated chain. Finding the right bucket takes
constant time. Traversing the chain takes at most K * e time, for some
constant e. So, searching for the element takes at most:
e * K + O (1), e >= 0
{definition of O()} <= e * K + d * 1, e >= 0, d >= 0
{arithmetic} == (e * K + d) * 1, e >= 0, d >= 0
{c == e * K + d} == c * 1
{c > 0} == O (1).
q.e.d.
I won't deny the performance will be rather lousy, but it's still
O (1). Which proves that big-Oh doesn't say everything.
- [1]
- Cormen, Leiserson, and Rivest: Introduction to Algorithms.
MIT Press, 1990. pp 26.
- [2]
- Knuth: The Art of Computer Programming, Volume 1:
Fundamental Algorithms. Third Edition. Addison-Wesley, 1997.
pp 107.
- [3]
-
- Sedgewick, and Flajolet: Analysis of Algorithms.
Addison-Wesley, 1996. pp 4.
Abigail