.net中有排序和搜索功能吗

发布于 2024-09-07 02:28:44 字数 96 浏览 7 评论 0原文

我正在对集合中的搜索实现线性搜索,然后我想为什么不使用二分搜索,为此我必须使用排序。 虽然我可以实现它们,但我想知道它们存在于.net本身的哪里。我希望它会出现在.Net 中。

I was implementing linear search for search in a collection then i thought why not use binary search, for which i had to use sorting.
Although I can implement them, but I would like to know where does they exists in .net itself. I hope it would be present in .Net.

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

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

发布评论

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

评论(3

葬花如无物 2024-09-14 02:28:44

如果我没有理解错的话,这就是您正在寻找的“

public class Person
{
    public int age;
    public string name;

    public Person(int age, string name)
    {
        this.age = age;
        this.name = name;
    }
}


List<Person> people = new List<Person>();

people.Add(new Person(50, "Fred"));
people.Add(new Person(30, "John"));
people.Add(new Person(26, "Andrew"));
people.Add(new Person(24, "Xavier"));
people.Add(new Person(5, "Mark"));
people.Add(new Person(6, "Cameron"));

搜索

 // with delegate
    List<Person> young = people.FindAll(delegate(Person p) { return p.age < 25; });

    // with lammda
    List<Person> young = people.FindAll(a=> a.age < 25);

排序”

// with delegate
people.Sort(delegate(TestKlasse a, TestKlasse b) { return a.age.CompareTo(b.age); });

// with lambda function
people.Sort((a, b) => a.age.CompareTo(b.age));

If I didn't get it wrong this is something you are looking for

public class Person
{
    public int age;
    public string name;

    public Person(int age, string name)
    {
        this.age = age;
        this.name = name;
    }
}


List<Person> people = new List<Person>();

people.Add(new Person(50, "Fred"));
people.Add(new Person(30, "John"));
people.Add(new Person(26, "Andrew"));
people.Add(new Person(24, "Xavier"));
people.Add(new Person(5, "Mark"));
people.Add(new Person(6, "Cameron"));

For Searching

 // with delegate
    List<Person> young = people.FindAll(delegate(Person p) { return p.age < 25; });

    // with lammda
    List<Person> young = people.FindAll(a=> a.age < 25);

For Sorting

// with delegate
people.Sort(delegate(TestKlasse a, TestKlasse b) { return a.age.CompareTo(b.age); });

// with lambda function
people.Sort((a, b) => a.age.CompareTo(b.age));
三寸金莲 2024-09-14 02:28:44

来自这个所以答案:

.NET 使用快速排序的变体(Sedgewick 的中位数为 3 的快速排序)。

From this SO answer:

.NET uses a variation of Quicksort (Sedgewick's median of 3 Quicksort).

彡翼 2024-09-14 02:28:44

.NET 默认实现排序,但是虽然您可以指定元素的排序顺序,但无法指定排序的算法。

因此,您可能对这篇文章感兴趣,它提供了 .NET 代码抽象排序包括每个主要排序方法的实现。

.NET has sorting implemented by default, but while you can specify the order in which you would like your elements sorted, you cannot specify the algorithm with which it sorts.

As such, you might be interested in this article which provides .NET code for abstractly sorting including implementations of every major sorting method.

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