繁体   English   中英

Enumerable.SortBy-使用哪种排序功能?

[英]Enumerable.SortBy - Which sorting function is used?

谁能告诉我Linq.Sort by使用哪种排序技术(气泡,插入,选择)。

编辑:是否也很容易进入该函数并亲自查看源代码-我以前能够使用Java做到这一点

没有LINQ Enumerable.SortBy方法。

但是,有一个Enumerable.OrderBy函数,该函数实例化使用EnumerableSorter类的新OrderedEnumerable 这些类的源代码如下。 欢迎您随便给他们打电话。

internal abstract class EnumerableSorter<TElement>
{
    // Methods
    protected EnumerableSorter()
    {
    }

    internal abstract int CompareKeys(int index1, int index2);
    internal abstract void ComputeKeys(TElement[] elements, int count);
    private void QuickSort(int[] map, int left, int right)
    {
        do
        {
            int index = left;
            int num2 = right;
            int num3 = map[index + ((num2 - index) >> 1)];
            do
            {
                while ((index < map.Length) && (this.CompareKeys(num3, map[index]) > 0))
                {
                    index++;
                }
                while ((num2 >= 0) && (this.CompareKeys(num3, map[num2]) < 0))
                {
                    num2--;
                }
                if (index > num2)
                {
                    break;
                }
                if (index < num2)
                {
                    int num4 = map[index];
                    map[index] = map[num2];
                    map[num2] = num4;
                }
                index++;
                num2--;
            }
            while (index <= num2);
            if ((num2 - left) <= (right - index))
            {
                if (left < num2)
                {
                    this.QuickSort(map, left, num2);
                }
                left = index;
            }
            else
            {
                if (index < right)
                {
                    this.QuickSort(map, index, right);
                }
                right = num2;
            }
        }
        while (left < right);
    }

    internal int[] Sort(TElement[] elements, int count)
    {
        this.ComputeKeys(elements, count);
        int[] map = new int[count];
        for (int i = 0; i < count; i++)
        {
            map[i] = i;
        }
        this.QuickSort(map, 0, count - 1);
        return map;
    }
}

internal class EnumerableSorter<TElement, TKey> : EnumerableSorter<TElement>
{
    // Fields
    internal IComparer<TKey> comparer;
    internal bool descending;
    internal TKey[] keys;
    internal Func<TElement, TKey> keySelector;
    internal EnumerableSorter<TElement> next;

    // Methods
    internal EnumerableSorter(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending, EnumerableSorter<TElement> next)
    {
        this.keySelector = keySelector;
        this.comparer = comparer;
        this.descending = descending;
        this.next = next;
    }

    internal override int CompareKeys(int index1, int index2)
    {
        int num = this.comparer.Compare(this.keys[index1], this.keys[index2]);
        if (num == 0)
        {
            if (this.next == null)
            {
                return (index1 - index2);
            }
            return this.next.CompareKeys(index1, index2);
        }
        if (!this.descending)
        {
            return num;
        }
        return -num;
    }

    internal override void ComputeKeys(TElement[] elements, int count)
    {
        this.keys = new TKey[count];
        for (int i = 0; i < count; i++)
        {
            this.keys[i] = this.keySelector(elements[i]);
        }
        if (this.next != null)
        {
            this.next.ComputeKeys(elements, count);
        }
    }
}

您可以使用.NET Reflector来读取已编译.NET程序集的源代码。

在反射器中查找后, OrderBy LINQ操作员使用快速排序。

internal abstract class EnumerableSorter<TElement>
{
    // Methods
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    protected EnumerableSorter();
    internal abstract int CompareKeys(int index1, int index2);
    internal abstract void ComputeKeys(TElement[] elements, int count);
    private void QuickSort(int[] map, int left, int right);
    internal int[] Sort(TElement[] elements, int count);
}

方法名称QuickSort使其很明显,并且算法对其进行了确认。

要检查装配体,请使用ILSpy之类的工具(通常的名称是反射器,但是在下一版本中将不再免费,并且所有免费版本均包含基于时间的禁用开关,而最后一个免费版本将于2011年3月的某个时间停用)。

Enumrable.OrderBy的文档仅说明

此方法执行稳定的排序; 也就是说,如果两个元素的键相等,则保留元素的顺序。

这意味着不同版本的.NET Framework的实现可能有所不同,但是假定它为O(n log n)。

编辑
请注意, Array.Sort的文档明确提到了快速排序的使用(并指出它是不稳定的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM