Why is accessing an element of a dictionary by key O(1) even though the hash function may not be O(1)?

后端 未结 8 1123
独厮守ぢ
独厮守ぢ 2020-12-13 23:53

I see how you can access your collection by key. However, the hash function itself has a lot of operations behind the scenes, doesn\'t it?

Assuming you have a nice h

8条回答
  •  孤街浪徒
    2020-12-13 23:55

    Please see post What does "O(1) access time" mean?

    The number of operations in a hash function is irrelevant as long as it takes the same (constant) amount of time for EVERY element in the collection. For example, accessing one element in a collection of 2 elements takes .001 ms, but also accessing one element in a collection of 2,000,000,000 elements takes .001 ms. Although the hash function can contain hundreds of if statements and multiple calculations.

提交回复
热议问题