在圆形数组中搜索
在圆形数组中搜索的最佳方法是什么?
Example 1 array : 45 67 44 11 49 4 56 12 39 90
circular array 11, 49, 4, 56, 12, 39, 90, 45, 67
二分搜索是正确的开始方法吗?
What is the best way to search in a circular array?
Example 1 array : 45 67 44 11 49 4 56 12 39 90
circular array 11, 49, 4, 56, 12, 39, 90, 45, 67
Is Binary search the right approach to start with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
二分查找仅在数组已排序时才有用。
您没有提供有关问题域的太多信息,但一种方法是使用集合(或哈希表)。对于放入数组中的每个数字,也将其插入集合中。集合(或哈希表)中的查找在恒定时间内发生,因此没有“搜索”。当您从数组中删除一个项目时,也会将其从集合中删除。如果循环缓冲区在填满时覆盖值,请确保它也更新该集以删除覆盖的值。
如果您无法使用其他数据结构,那么您能做的最好的事情就是对数组进行线性扫描。
Binary search is only useful if the array is sorted.
You didn't provide much info about the problem domain but one approach would be to use a set (or hash table). For every number you put in the array, also insert it in the set. Lookups in a set (or hash table) happen in constant time, so there's no "searching". When you remove an item from the array, also remove it from the set. If your circular buffer overwrites values as it fills up, make sure it also updates the set to remove overwritten values.
If you can't use another data structure, then the best you can do is a linear scan of the array.
遇到了同样的问题,无法找到一种在不运行搜索两次的情况下使用内置函数的方法,因此编写了一个自定义函数。
可能有一种方法可以更快地进行超出范围检查,但这符合我的目的。
(不想复制带有负索引内容的标准二进制搜索接口,因为消费者将其转换回循环缓冲区上的真实索引会很痛苦)
Had this same problem, couldn't see a way to use builtin functions without running the search twice so wrote a custom one.
There is probably a way to do the out of range check quicker, but this serves my purpose.
(Didn't want to copy the standard binary search interface with the negative index stuff as the consumer converting it back to real indexes on a circular buffer would have been painful)