Finding data fast

You know that searching for a key in an unsorted array or a linked list has time cost O(N) average case

You know that searching for a key in a sorted array or a balanced search tree or a randomized search tree or a skip list has time cost O(logN) average case

Is it possible to do better?...

Consider searching for a key
k
in an array
a
. If we somehow knew that, if key
k
is in the array, it would be at index
i
, we could do a find on
k
with one array access:
a[i]

This would be O(1), ignoring any memory hierarchy effects

Of course it may be unreasonable to just "know" the index for key
k
. But suppose we could compute the index for
k
in time O(1). Then we could still do a search with time cost O(1)!

Hashing
and
hash tables
are ways of trying to make this simple idea a reality

... and it turns out that with good design it is definitely possible to get O(1) average case find, insert, and delete time costs, and good space costs too
CONTENTS PREVIOUS NEXT