怎么叫Icomparer.Compare()方法时,排序的实现? [英] How is Icomparer.Compare() method called when Sort is implemented?

查看:170
本文介绍了怎么叫Icomparer.Compare()方法时,排序的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 谁叫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:<一href="http://en.wikipedia.org/wiki/Insertion_sort#mediaviewer/File%3aInsertion-sort-example-300px.gif" rel="nofollow">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

这篇关于怎么叫Icomparer.Compare()方法时,排序的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆