在 .NET/C# 上下文中,什么是二分搜索以及如何/为什么使用二分搜索?

发布于 2024-09-09 08:38:40 字数 164 浏览 6 评论 0原文

我今天第一次在维基百科上读到有关二分搜索的内容,只是粗略地浏览了一下。它似乎用于在内存稀疏的情况下快速查找集合中的项目。

在 .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 技术交流群。

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

发布评论

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

评论(3

情绪少女 2024-09-16 08:38:40

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.

享受孤独 2024-09-16 08:38:40

二分搜索仅适用于排序数据,因此只要您在 C# 中有一些已知已排序的数据集合,就可以对其进行二分搜索。您最好的选择是使用已提供的实现(例如 List.BinarySearch()),但如果您使用的特定集合没有二分搜索方法,你总是可以写一个。

下面是一个使用内置库的示例:

// An ordered list of ints
List<int> ints = new List<int>() { 1, 4, 8, 20, 30, 44 };

// Search for 5 in the list
int ix = ints.BinarySearch(8);

// Display the index the element 8 was found at
Console.WriteLine(ix);

是的,当您搜索排序数据时,您肯定会希望使用二分搜索。

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:

// An ordered list of ints
List<int> ints = new List<int>() { 1, 4, 8, 20, 30, 44 };

// Search for 5 in the list
int ix = ints.BinarySearch(8);

// Display the index the element 8 was found at
Console.WriteLine(ix);

And yes, you would definitely want to use a binary search when you're searching sorted data.

凉栀 2024-09-16 08:38:40

不久前(当我还是计算机工程课程的学生时)我必须使用搜索算法。这种课程作业的结果就是一篇论文。

我在我的博客上发布了关于 快速排序和二分搜索算法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.

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