Machine-translated by Google. Questions are authored in English.
Top

Discussion

In a hash table with 'n' slots using chaining for collision resolution, what is the average time complexity for search in the worst case ?

  • A.O(1)
  • B.O(log n)
  • C.O(n)
  • D.\(O(n^{2})\)

Answer: C

In the worst case a search takes O(n) time.

With separate chaining each slot holds a linked list of the keys that hash to it. If the hash function sends all n keys to the same slot, one chain holds every key, and a search has to walk that whole list, which is linear time.

O(1) is the average case with a good hash function and a bounded load factor, not the worst case. O(log n) applies only if each chain is replaced by a balanced tree. \(O(n^{2})\) is too large, since a search visits each key at most once.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.