asp.net:在字典中按键搜索值的时间复杂度是常数时间还是log2(n)?

发布于 2024-07-20 02:35:20 字数 77 浏览 4 评论 0原文

我需要 dotnet 中的一个数据结构,我可以在恒定时间内搜索一个项目。这意味着数据结构应该在内部实现索引。字典对于此目的或其他目的有用吗?

i need a datastructure in dotnet where i can search an item in constant time.it means that datastructure should implement indexing internally.is dictionary usefull for this purpose or some other one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2024-07-27 02:35:20

是的,使用字典<> (如果您使用的是旧版本的 .NET,则为 Hashtable)。 确保您填充到字典中的对象具有良好的哈希值(针对您在字典中用作键的对象,研究覆盖 GetHashCode() 和 Equals())。 如果您的数据对象的哈希码性能较差,性能就会开始下降。 是的,为了回答你的问题,在哈希表/字典中查找应该是相对恒定的时间(书籍通常说它是 O(1),但这是有争议的)。 查找性能将由许多因素决定:

  1. 哈希函数(决定分布)
  2. 表如何处理冲突? 试探? 水桶? (大多数实现都使用存储桶)。
  3. 桌子可以调整大小吗? 它如何调整大小? 小增量? 2 的幂? 质数?
  4. ETC...

Yes, use Dictionary<> (or Hashtable if you are on an older version of .NET). Be sure the objects you are stuffing in the dictionary have a good hash value (look into overriding GetHashCode() and Equals() for your objects you are using as keys in the Dictionary). If your data objects have poor hash codes performance will start to degrade. And yes, to answer your question, looks ups in a hashtable/dictionary should be relatively constant time (books generally say it is O(1), but that's arguable). The lookup performance will be determined by many factors:

  1. Hash function (determines distribution)
  2. How does the table handle collisions? Probing? Buckets? (most implementations use buckets).
  3. Is the table resizable? How does it resize? Small increments? Power of 2? Prime numbers?
  4. etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文