加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Array.Sort是如何在.NET中实现的?

发布时间:2020-12-17 07:27:03 所属栏目:百科 来源:网络整理
导读:我在编程中使用结构,并使用IComparer根据结构中的值对结构进行排序. Microsoft如何实现Array.Sort()方法?有没有这方面的文件(参考)?对于Visual Basic中的所有类型的Sort(),它是否相同? 这是我想要的一个简单的例子. Dim MyArray(6) As Integer MyArray(0)
我在编程中使用结构,并使用IComparer根据结构中的值对结构进行排序.

Microsoft如何实现Array.Sort()方法?有没有这方面的文件(参考)?对于Visual Basic中的所有类型的Sort(),它是否相同?

这是我想要的一个简单的例子.

Dim MyArray(6) As Integer
    MyArray(0) = 1
    MyArray(1) = 45
    MyArray(2) = 45
   ' Some Code.....
    '.........
    '..........
    MyArray(3) = 1
    MyArray(4) = 10
    ' Some Code.....
    '.........
    '..........
    MyArray(5) = 1
    MyArray(6) = 57

    Array.Sort(MyArray)

Array.Sort()将此数组排序为:(1 1 1 10 45 45 57)

1号怎么排序?它是将第一个结束还是保留在同一个索引中?

在我的原始示例中(排序之前),MyArray(0)= 1并且在排序MyArray(0)= 1之后.

这是相同的原始1或另一个1(添加到阵列的最新的一个)移动到那个位置?

如果排序后MyArray(0)= 1,则在排序之前应该是MyArray(5)= 1.

解决方法

Array.Sort是一种不稳定的排序,因此相同元素的顺序是未定义的而不是守恒的. MSDN中的 Array.Sort文章指出:

This method uses the QuickSort algorithm. This implementation performs an unstable sort; that is,if two elements are equal,their order might not be preserved. In contrast,a stable sort preserves the order of elements that are equal.

另一方面,LINQ的OrderBy方法是稳定的. OrderBy in the MSDN的文章指出:

This method performs a stable sort; that is,if the keys of two elements are equal,the order of the elements is preserved. In contrast,an unstable sort does not preserve the order of elements that have the same key.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读