Which is faster to find an item in a hashtable or in a sorted list?

后端 未结 7 2050
栀梦
栀梦 2020-12-24 06:41

Which is faster to find an item in a hashtable or in a sorted list?

7条回答
  •  生来不讨喜
    2020-12-24 07:34

    Unless the hashing algorithm is extremely slow (and/or bad), the hashtable will be faster.

    UPDATE: As commenters have pointed out, you could also be getting degraded performance from too many collisions not because your hash algorithm is bad but simply because the hashtable isn't big enough. Most library implementations (at least in high-level languages) will automatically grow your hashtable behind the scenes—which will cause slower-than-expected performance on the insert that triggers the growth—but if you're rolling your own, it's definitely something to consider.

提交回复
热议问题