二分查找是否具有 deque C++ 的对数性能?数据结构?

发布于 2024-11-16 20:49:00 字数 293 浏览 4 评论 0原文

标准规定 std::binary_search(...) 以及两个相关函数 std::lower_bound(...)std::upper_bound( ...) 如果数据结构具有随机访问,则为 O(log n)。因此,鉴于此,我假设这些算法在 std::deque 上具有 O(log n) 性能(假设其内容由用户排序)。

然而,似乎 std::deque 的内部表示很棘手(它被分成了块),所以我想知道: O(log n) 搜索的要求是否适用于 std ::双端队列。

The standard says that std::binary_search(...) and the two related functions std::lower_bound(...) and std::upper_bound(...) are O(log n) if the data structure has random access. So, given that, I presume that these algorithms have O(log n) performance on std::deque (assuming its contents are kept sorted by the user).

However, it seems that the internal representation of std::deque is tricky (it's broken into chunks), so I was wondering: does the requirement of O(log n) search hold for std::deque.

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

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

发布评论

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

评论(3

余生共白头 2024-11-23 20:49:00

是的,它仍然适用于deque,因为容器需要在常数时间内提供对任何元素的访问(只是比vector稍高的常数)。

但这并不能免除您对双端队列进行排序的义务。

Yes it still holds for deque because the container is required to provide access to any element in constant time (just a slightly higher constant than vector).

That doesn't relieve you of the obligation for the deque to be sorted though.

你好,陌生人 2024-11-23 20:49:00

是的。双端队列对索引(如果存在)具有恒定时间访问权限。它被组织成大小相等的页面。你所拥有的是某物。就像指向页面的指针向量。如果您有 2 个包含 100 个元素的页面,并且您想要访问第 103 个元素,则通过 103/100 = 1 确定页面并通过 103 %100 => 确定页面中的索引3. 现在在两个向量中使用恒定时间访问来获取元素。

这是来自 http://www.cplusplus.com/reference/stl/deque/:

双端队列可以由特定的库以不同的方式实现,但在所有情况下,它们都允许通过随机访问迭代器访问各个元素,并且存储始终自动处理(根据需要扩展和收缩)。

Yes. deque has constant time access for an index if it is there. It is organized in pages of an equal size. What you have is smth. like a vector of pointers to pages. If you have let's say 2 pages with 100 elements and you would like to access 103rd element than determine the page by 103/100 = 1 and determine the index in the page by 103 %100 => 3. Now use a constant time access in two vectors to get the element.

Here the statement from http://www.cplusplus.com/reference/stl/deque/:

Deques may be implemented by specific libraries in different ways, but in all cases they allow for the individual elements to be accessed through random access iterators, with storage always handled automatically (expanding and contracting as needed).

开始看清了 2024-11-23 20:49:00

写一个程序来测试一下,我认为deque的binary_search实现会比vector慢,但它的复杂度仍然是O(logn)

Just write a program to test that, I think deque's implementation of binary_search will slower than vector's, but it's complexity is still O(logn)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文