大头文

分享,进步

冒泡排序C#实现,使用委托,包括三种方式:Fun<>,匿名方法,Lambda表达式

冒泡排序是一种简单的排序方法,适合于小量数字排序,对于大量数字(超过10个),还有更高效的排序方法。
这里的实现的冒泡排序,需实现功能:
不仅数字排序,还要对任意对象排序

示例:

  • 对People对象的Age(年龄)排序
  • 对Student对象的Score(分数)排序

People:

    public class People
{
public string Name { get; set; }
public int Age { get; set; }
public People(string name, int age)
{
Name = name;
Age = age;
}
public static bool CompareAge(People p1, People p2)
{
return p1.Age < p2.Age;
}
public override string ToString()
{
return string.Format("[people] name:{0},age:{1}", Name, Age);
}
}

Student:

    public class Student
{
public string Name { get; set; }
public float Score { get; set; }
public Student(string name, float score)
{
Name = name;
Score = score;
}
public static bool CompareScore(Student s1, Student s2)
{
return s1.Score < s2.Score;
}
public override string ToString()
{
return string.Format("[student] name:{0},score:{1}",Name,Score);
}
}

冒泡排序的实现,如果对Fun<>的委托写法不懂,最后有说明。

    public static class Storter_Fun
{
public static void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparision)
{
bool swapped = true;
do
{
swapped = false;
for (int i = 0; i < sortArray.Count - 1; i++)
{
if (comparision(sortArray[i + 1], sortArray[i]))
{
T temp = sortArray[i];
sortArray[i] = sortArray[i + 1];
sortArray[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
}
}

控制台,使用排序:

        static void Main(string[] args)
{
//人按年龄排序
People[] peoples = {new People("张三",43),
new People("李四",12),
new People("王五",50),
new People("吴六",21),
new People("陈七",33),};
//Fun<>委托的写法
BubbleSorter.Storter_Fun.Sort<People>(peoples, People.CompareAge);
//匿名方法的写法
BubbleSorter.Storter_Fun.Sort<People>(peoples, delegate(People p1, People p2)
{
return p1.Age < p2.Age;
});
//Lambdah表达式的写法
BubbleSorter.Storter_Fun.Sort<People>(peoples, (People p1, People p2) =>
{
return p1.Age < p2.Age;
});
foreach (var people in peoples)
{
Console.WriteLine(people);
}
//学生按分数排序
Student[] students = {new Student("张三",80.5F),
new Student("李四",85),
new Student("王五",88),
new Student("吴六",70),
new Student("陈七",95),};
BubbleSorter.Storter_Fun.Sort<Student>(students, Student.CompareScore);
foreach (var student in students)
{
Console.WriteLine(student);
}
Console.ReadKey();
}

委托:调用方法的参数一般情况是数值型的,例如int,string,DataTable等等,能不能将方法本身作为一个参数传递给另一个方法呢?委托就是解决这个问题的。
既然是参数,要就有类型。C#是强类型,方法的参数是int类型,就不能传string类型进去,否则在编辑阶段就报错了。
int,string等是已有的类型,委托的类型就需要自定义。
例如上面例子用到的:

  • Fun<t, t,="" bool=""> 代表方法是传入两个泛型参数,返回bool;
  • Fun<int,string,datatable,array> 代表方法是传入三个参数:int, string,DataTable,返回Array;
    就是最后一个参数为返回值类型;
    那么如果没有返回值呢?
  • Action<t,t> 代表方法是传入两个泛型参数,没返回值
  • Action<int,string> 代表方法是传入两个参数:int,string,没返回值
    定义了委托的类型,传入的方法必须要符合这个定义,即:传入参数,类型,个数,顺序,返回值要一致,否则编译不过,C#是强类型的原因。

刚实现的冒泡可以支持不同对象的排序,主要是将冒泡比较大小方法传递进来(委托),与交换数据的临时变量使用了泛型实现;

 
分类: C#

最新文章

  1. 关于MapReduce中自定义带比较key类、比较器类(二)——初学者从源码查看其原理
  2. Android零散
  3. 10 件有关 JavaScript 让人费解的事情
  4. [转]eclipse最佳设置
  5. jsp网站服务器配置
  6. 数据库开发及ADO.NET
  7. Enabling CORS in WCF
  8. CentOS7编译安装Nginx-1.8.1和编译参数
  9. timus 1136 Parliament(二叉树)
  10. C#_字符串的操作
  11. JS实现刷新iframe的方法
  12. HDU 3360 National Treasures
  13. android HorizontalListView
  14. Cron 时间元素
  15. dubbo源码分析(一)
  16. mybatis缓存机制
  17. 面试----你可以手写一个promise吗
  18. 一次练习 发现的问题,malloc free 无效;findfirstfile失败,writefile失败的原因
  19. github git 无法读取远程仓库或无权限
  20. testng入门教程5TestNG套件测试

热门文章

  1. net搭建热插拔式web框架
  2. 【 D3.js 进阶系列 — 1.1 】 其它表格文件的读取
  3. 从&amp;quot;分层二进制输出&amp;quot;至&amp;quot;解决二进制树深度&amp;quot;总结
  4. Could not drop object &amp;#39;student&amp;#39; because it is referenced by a FOREIGN KEY constraint
  5. CSDN Androidclient生产 导航帖
  6. Android UI法宝发展Angrytools
  7. java中float/double浮点数的计算失精度问题(转)
  8. 理解JNDI中 java:comp/env/jdbc/datasource 与 jdbc/datasource 的不同之处(转)
  9. H.265/HEVC Codec 编解码 (MP4 和 TS)
  10. Android 按下电源按钮关闭小学习过程的整个长度