请问关于这个排序的函数,他的内部执行机制是怎样的?
譬如:foreach(int x in arr){...}
它的内部是实现:
IEnumerator enumerator=arr.Getnumerator();
while(enumerator.MoveNext())
{int x=(int)enumerator.Current;}而关于这个Array.sort()是怎样的?我只知道它要执行的是接口IComparer和IComparable,但是具体怎么执行接口里的方法就不清楚了,请教请教

解决方案 »

  1.   

    此方法使用 QuickSort 算法。此实现执行不稳定排序;亦即,如果两元素相等,则其顺序可能不被保留。相反,稳定排序保留相等元素的顺序。一般情况下,此方法的运算复杂度为 O(n log n),其中 n 是 array 的 Length;最坏的情况下其运算复杂度为 O(n ^ 2)。
      

  2.   

    我觉得这个例子说的很清楚了:)using System;
    using System.Collections;public class SamplesArray  {
     
       public class myReverserClass : IComparer  {      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
          int IComparer.Compare( Object x, Object y )  {
              return( (new CaseInsensitiveComparer()).Compare( y, x ) );
          }   }   public static void Main()  {
     
          // Creates and initializes a new Array and a new custom comparer.
          String[] myArr = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" };
          IComparer myComparer = new myReverserClass();
     
          // Displays the values of the Array.
          Console.WriteLine( "The Array initially contains the following values:" );
          PrintIndexAndValues( myArr );
     
          // Sorts a section of the Array using the default comparer.
          Array.Sort( myArr, 1, 3 );
          Console.WriteLine( "After sorting a section of the Array using the default comparer:" );
          PrintIndexAndValues( myArr );      // Sorts a section of the Array using the reverse case-insensitive comparer.
          Array.Sort( myArr, 1, 3, myComparer );
          Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );
          PrintIndexAndValues( myArr );      // Sorts the entire Array using the default comparer.
          Array.Sort( myArr );
          Console.WriteLine( "After sorting the entire Array using the default comparer:" );
          PrintIndexAndValues( myArr );      // Sorts the entire Array using the reverse case-insensitive comparer.
          Array.Sort( myArr, myComparer );
          Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );
          PrintIndexAndValues( myArr );   }
     
       public static void PrintIndexAndValues( String[] myArr )  {
          for ( int i = 0; i < myArr.Length; i++ )  {
             Console.WriteLine( "   [{0}] : {1}", i, myArr[i] );
          }
          Console.WriteLine();
       }
    }
    /* 
    This code produces the following output.The Array initially contains the following values:
       [0] : The
       [1] : QUICK
       [2] : BROWN
       [3] : FOX
       [4] : jumps
       [5] : over
       [6] : the
       [7] : lazy
       [8] : dogAfter sorting a section of the Array using the default comparer:
       [0] : The
       [1] : BROWN
       [2] : FOX
       [3] : QUICK
       [4] : jumps
       [5] : over
       [6] : the
       [7] : lazy
       [8] : dogAfter sorting a section of the Array using the reverse case-insensitive comparer:
       [0] : The
       [1] : QUICK
       [2] : FOX
       [3] : BROWN
       [4] : jumps
       [5] : over
       [6] : the
       [7] : lazy
       [8] : dogAfter sorting the entire Array using the default comparer:
       [0] : BROWN
       [1] : dog
       [2] : FOX
       [3] : jumps
       [4] : lazy
       [5] : over
       [6] : QUICK
       [7] : the
       [8] : TheAfter sorting the entire Array using the reverse case-insensitive comparer:
       [0] : the
       [1] : The
       [2] : QUICK
       [3] : over
       [4] : lazy
       [5] : jumps
       [6] : FOX
       [7] : dog
       [8] : BROWN*/
      

  3.   

    我想知道的是,例如在你给的例子中:执行Array.Sort( myArr, 1, 3 );那它没有怎么去调用int IComparer.Compare( Object x, Object y ) 的?这个过程是怎样的?
      

  4.   

    我想要的是如:
    譬如:foreach(int x in arr){...} 
    它的内部是实现: 
    IEnumerator enumerator=arr.Getnumerator(); 
    while(enumerator.MoveNext()) 
    {int x=(int)enumerator.Current;} 
    。。
    那么调用Array.Sort( myArr, 1, 3 ); 
    它的内部是怎么实现的??我想从IComparer和ICompartor两个接口中分别知道是怎么来实现的??
      

  5.   

    IComparer接口中的Compare方法是比较两个对象并返回一个值,指示一个对象是小于、等于还是大于另一个对象。这个方法其实就是一个比较的方法,你所说的调用就是调用的这个方法而已.不清楚你想问什么.上例中实现了这个接口,实现了Compare方法,其中CaseInsensitiveComparer()是比较两个对象是否相等,比较时忽略字符串的大小写。
    public class myReverserClass : IComparer  {      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
          int IComparer.Compare( Object x, Object y )  {
              return( (new CaseInsensitiveComparer()).Compare( y, x ) );
          }   }说了这么多.不知道你明白没有.-_-!
      

  6.   

    你的意思是当你写代码:Array.sort(aa);//aa是一个数组那么编译器就会隐式得调用IComparer.Compare( Object x, Object y )吗?
    那它的具体过程是怎样的?譬如:foreach(int x in arr){...} //arr是一个迭代器
    它的内部是实现: 
    IEnumerator enumerator=arr.Getnumerator(); 
    while(enumerator.MoveNext()) 
    {int x=(int)enumerator.Current;} 那么调用Array.sort(aa);//aa是一个数组
    以后,它的内部又是怎样的?不好意思,可能是我的表述有点问题