怎么叫Icomparer.Compare()方法时,排序的实现?方法、Icomparer、Compare

2023-09-06 11:02:30 作者:Chafferer · 迷心

谁叫Icomparer.Compare()方法?

考虑下面的示例。

Array.Sort(Myfiles, new customSort())
.
.
.
private class customSort : IComparer<object>
{
 public override int Compare(obj A, obj B)
 {
   return (A.Value==B.Value?(0):((A.Value < B.Value)?(-1):(1)));
 }
}

如果Myfiles中包含的对象集合,

If Myfiles contain collection of objects,

如何在A和B决定。 是A和B的连续对象中日Myfiles中集? 如果不是,怎么会是A和B Myfiles中选择? 请问的Array.Sort()方法来用它做什么? How are A and B decided. Are A and B consecutive objects in th Myfiles collection? If not, How is A and B selected from Myfiles? Does the Array.Sort() method has to do anything with it?

推荐答案

此方法使用内省排序(introsort)算法如下:

This method uses the introspective sort (introsort) algorithm as follows:

如果分区大小为少于16个元素,它使用的插入排序算法。 如果分区的数目超过2 * LOGN,其中N是输入数组的范围,它采用了堆排序算法。 否则,它采用的是快速排序算法。

从 http://msdn.microsoft.com/en-us/library /kwx6zbd4.aspx

有关在这里第一个选项是一个很好的gif:http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File:Insertion-sort-example-300px.gif

For first option here is a nice gif: http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File:Insertion-sort-example-300px.gif