在 .NET/C# 上下文中,什么是二分搜索以及如何/为什么使用二分搜索?
我今天第一次在维基百科上读到有关二分搜索的内容,只是粗略地浏览了一下。它似乎用于在内存稀疏的情况下快速查找集合中的项目。
在 .NET/C# 环境中,我是否需要使用它?您在构建实际的生产软件时使用过它们吗?
如果这个问题听起来很煽动,我很抱歉,但我作为一名学生问的是一个真实的问题!
I've read about binary searches on Wikipedia for the first time today and just skimmed the surface a bit. It seems it's used to find items in a collection quickly where memory is sparse.
In a .NET/C# context, would I ever need to use one? Do you ever use them while building production actual-real-world software?
I'm sorry if this questions comes off as inciting, but I'm asking a genuine question as a student!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
List
有一个 BinarySearch方法,Array 也是如此。如果您有一个排序列表并且需要查找一个元素,您将使用它们。因为它们返回一个索引,所以您可以完成使用直接字典无法完成的操作,例如查找小于键的最大元素。我在现实软件中使用二分搜索的一个地方是进行范围搜索。运费是根据重量范围给出的,因此 0-1 磅可能有一种费率,1-5 磅有一种费率,5-10 磅有一种费率。如果我调用
List.BinarySearch
并查找 4 磅,它会给我第一个高于 4 磅的索引,我可以使用它来查找 1-5 磅范围。字典只会告诉我没有找到 4 磅。对于一般排序数据,您通常最好使用 SortedList 或 < a href="http://msdn.microsoft.com/en-us/library/f7fta44c.aspx" rel="nofollow noreferrer">排序字典。
List<T>
has a BinarySearch method, as does Array. You would use them if you had a sorted list and needed to find an element. Because they return an index, you can do things you can't with a straight dictionary like find the largest element less than a key.One place I've used a binary search in real-world software is for doing range searches. Shipping rates are given for weight range, so there might be one rate for 0-1 lb, one for 1-5 lb, and one for 5-10 lb. If I call
List<T>.BinarySearch
and look for 4 lb, it will give me the first index higher than 4 lb, which I can use the find the 1-5 lb range. A dictionary would just tell me that 4 lb was not found.For general sorted data, you are often better off using SortedList or SortedDictionary.
二分搜索仅适用于排序数据,因此只要您在 C# 中有一些已知已排序的数据集合,就可以对其进行二分搜索。您最好的选择是使用已提供的实现(例如
List.BinarySearch()
),但如果您使用的特定集合没有二分搜索方法,你总是可以写一个。下面是一个使用内置库的示例:
是的,当您搜索排序数据时,您肯定会希望使用二分搜索。
Binary searches only work on sorted data, so as long as you have some collection of data in C# that you know is sorted, you can do a binary search on it. Your best bet would be to use the implementations that are already provided (such as
List<T>.BinarySearch()
), but if the particular collection you're using doesn't have a binary search method, you can always write one.Here's an example using the built in libraries:
And yes, you would definitely want to use a binary search when you're searching sorted data.
不久前(当我还是计算机工程课程的学生时)我必须使用搜索算法。这种课程作业的结果就是一篇论文。
我在我的博客上发布了关于 快速排序和二分搜索算法C++。虽然它是 C++ 代码,但它应该为您提供如何/为什么使用二分搜索。它还展示了如何实现二分搜索算法。提供了一个示例应用程序,以便您可以自行测试。
Sometime ago (while I was a student in the Computer Engineering course) I had to work with search algorithms. The result of such coursework was a paper.
I posted on my blog about Quicksort and Binary Search algorithms in C++. Although it's in C++ code it should provide you with the how/why to use binary search. It also shows how to implement the binary search algorithm. There's a sample application provided so that you can test it by yourself.