• 冒泡排序


       冒泡一: 将一个字符串数组按照冒泡排序的方式进行排序

    public static int[] GetBubbbleSort( int[] ArrayList) 2 { 3 for (int i = 0; i < ArrayList.Length-1; i++) 4 { 5 for (int j = 0; j < ArrayList.Length-i-1; j++) 6 { 7 int temp; 8 //大于为从小到大,小于为从大到小 9 if (ArrayList[j]>ArrayList[j+1]) 10 { 11 temp = ArrayList[j]; 12 ArrayList[j] = ArrayList[j + 1]; 13 ArrayList[j+1] = temp; 14 } 15 } 16 } 17 return ArrayList; 18 }
    冒泡 排序二:
       public static int[] BubbleSotr(int[] ArrayList)
            {
                bool bubble = true;
                do
                {
                    bubble = false;
                    for (int i = 0; i < ArrayList.Length-1; i++)
                    {
                        if (ArrayList[i]>ArrayList[i+1])
                        {
                            ArrayList[i] = ArrayList[i] + ArrayList[i + 1];
                            ArrayList[i + 1] = ArrayList[i] - ArrayList[i + 1];
                            ArrayList[i]=ArrayList[i]-ArrayList[i+1];
      bubble = true; } } } while (bubble==true); return ArrayList; } //Bubble冒泡排序的第一种方式

      冒泡排序三:

    
    
        public static void Sort<T>(IList<T> arrayList, Func<T, T, bool> compareration)
            {
                bool bubble = true;
                do
                {
                    bubble = false;
                    for (int i = 0; i < arrayList.Count - 1; i++)
                    {
                      //传递一个与compareration一样的方法
                        if (compareration(arrayList[i], arrayList[i + 1]))
                        {
                            T atemp;
                            atemp = arrayList[i];
                            arrayList[i] = arrayList[i+1];
                            arrayList[i+1] = atemp;
                            bubble = true;
                        }
                    }
    
                } while (bubble == true);
    
            }

    Emploee类
    public
    class Emploee { public String Name { get; set; } public int Salary { get; set; } public Emploee(string name,int salary) { this.Name = name; this.Salary = salary; } public override string ToString() { return string.Format("Name:{0},Salary:{1}",Name,Salary); } public static bool MyCompareFuction(Emploee e1,Emploee e2) { return e1.Salary>e2.Salary; } }
    主函数:实现排序;
     class Program
        {
            static void Main(string[] args)
            {
                Emploee[] emploee = {
                new Emploee("张三",5000),
                new Emploee("李四",3000),
                new Emploee("王五",4500),
                new Emploee("赵六",3333)
             };
               Bubble.Sort<Emploee>(emploee,Emploee.MyCompareFuction);
               foreach (var item in emploee)
               {
                   Console.WriteLine(item);
               }
               Console.ReadKey();
             
            }
        }







  • 相关阅读:
    登录Mysql看不到Mysql库
    七牛云使用记录
    FFmpeg工具
    解决VMware下CentOS连不上网络问题
    14.中介者模式
    二十三种设计模式(三)
    23种设计模式(二)
    搭建ssm环境
    文件的字符流与字节流读写
    设计模式之用工厂模式实现计算器
  • 原文地址:https://www.cnblogs.com/xiaobing1/p/9943076.html
Copyright © 2020-2023  润新知